Passing parameters and calling of setTimeout and setInterval functions in JavaScript _ basic knowledge

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.