This article mainly introduces parameter passing and calling of setTimeout and setInterval functions in JavaScript, two functions can insert the code to be executed into a code queue maintained by the js engine at a set time point. For more information, see
How to pass parameters to setTimeout and setInterval
See the following code:
var str = 'aaa'; var num = 2; function auto(num){ alert(num); } setTimeout('auto(num)',4000);
In this way, the write operation works normally, but if it is said that this is a parameter transfer, it is better to say that it is a directly used global variable. Therefore, this writing method is unnecessary. Generally, it is used to pass local variables as parameters.
Modify the Code:
//var str = 'aaa'; var num = 2; function test(){ var str = 'bbb'; setTimeout('auto(str)',4000); } function auto(a){ alert(a); } test();
This write method reports an error. If you uncomment the global declaration on str, aaa is output, that is, the function still calls global variables.
See the following code:
//var str = 'aaa'; var num = 2; function test(){ var str = 'bbb'; setTimeout('auto("str")',4000); } function auto(a){ alert(a); } test();
Note that the str above will output "str". That is to say, after the timer calls the function, str is directly treated as a parameter. The passed parameter is always a string. This is not the expected result.
To pass unexpected parameters except strings, you can use the closure. See the following code:
//var str = 'aaa'; var num = 2; function test(){ var str = 'bbb'; setTimeout(auto(str),4000); } function auto(str){ return function(){ alert(str); } } test();
The output is 'bbb '. If auto (str) is enclosed in quotation marks, an error is returned.
Of course, this writing is also good:
var num = 2; function test(){ var str = 'bbb'; //setTimeout(auto(str),4000); setTimeout(function(){alert(str)},4000); } function auto(str){ return function(){ alert(str); } } test();
Note that when no closure is used to pass parameters, the function called by the timer must be enclosed by quotation marks. If no quotation marks are provided, an error is returned. The above situations are also suitable for setInterval ();
SetTimeout, setInterval function call
The following code is available:
var num = 2; function auto(){ alert(num); } setTimeout(auto(),4000);
In this program, the pop-up warning box is displayed immediately during the test. That is to say, if you reference a function using the above method, the timer does not work.
Similarly, the code above setInterval cannot work normally. The program can only pop up a warning box once and then report an error.
Change the timer
setInterval('auto()',4000); setTimeout('auto()',4000);
The program can work normally.
What if I only use auto () to call a function?
var str = 'aaa'; var num = 2; function auto(){ alert(num); } //setInterval(auto,4000); setTimeout(auto,4000);
In this way, programs can work normally;
If auto is enclosed in quotation marks
//setInterval('auto',4000); setTimeout('auto',4000);
Cannot work normally.