SetTimeout (code, millisec) method. If the code of the 1st parameter calls the function name directly, a global or local function call problem exists. Note the following when using the setTimeout (code, millisec) method:
SetTimeout (expression, interaction time)
The latency/interaction time is in the unit of haoss (1000 ms = 1 s). When setTimeout is executed, it is executed once after the specified delay time after loading, and only once
When setTimeout is executed, it executes the expression once every specified time after loading.
1. Basic usage:
Run a piece of code:
The Code is as follows: |
Copy code |
Var I = 0; SetTimeout ("I + = 1; alert (I)", 1000 ); |
Execute a function:
The Code is as follows: |
Copy code |
Var I = 0; SetTimeout (function () {I + = 1; alert (I) ;}, 1000 ); |
// Compare the two methods above.
SetTimeout ()
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Function test (){ Alert ("I Am a global "); } Function myfun (){ Function test (){ Alert ("I am local "); } SetTimeout ("test ()", 2000); // call the global test SetTimeout (test, 5000); // call the local test } Myfun (); </Script> |
The running result is that alert first outputs "I Am a global" and then alert outputs "I am a local "!