The setTimeout function is a scheduled function that can call a specified function every few seconds for operations. Below is an infinite loop instance, one is that the setTimeout function can pass the parameter/value passing instance, both of which have representative meanings, you can refer.
The window. setTimeout method is used to delay the execution of a function (method. However, it is defective when calling the specified method and passing parameters.
Infinite Loop
The Code is as follows: |
Copy code |
<Div id = "num"> </div> <Script type = "text/javascript"> Window. onload = function () { Var a = 0; Function addNumber () { A = a + 1; Document. getElementById ("num"). innerHTML =; Settime out (addNumber, 1000 ); } AddNumber (); } </Script> |
The setTimeout function can pass parameters/values.
The Code is as follows: |
Copy code |
Var _ st = window. setTimeout; // FRef is the test function, and mDelay is the time Window. setTimeout = function (fRef, mDelay ){ If (typeof fRef = 'function '){ Var argu = Array. prototype. slice. call (arguments, 2 ); Var f = ( Function (){ FRef. apply (null, argu ); }); Return _ st (f, mDelay ); } Return _ st (fRef, mDelay ); }
Function test (x, y, z ){ Alert (x + "" + y + "" + z ); }
|
The first parameter is the function to be executed, the second parameter is the interval, and the following parameter is the value to be passed in.
The Code is as follows: |
Copy code |
Window. setTimeout (test, 1000, document. getElementById ("d"), 'abc', '123 '); |