The basic knowledge of settimeout and SetInterval functions in JavaScript by reference and invocation

Source: Internet
Author: User
Tags closure setinterval

How to pass parameters to SetTimeout, setinterval
look at the following code:

var str = ' AAA '; 
var num = 2; 
function Auto (num) { 
  alert (num); 
} 
SetTimeout (' Auto (num) ', 4000); 

This is a good way to write, but as it says this is a parameter pass, it's more of a global variable that is used directly. Therefore, this writing is not necessary, in general, more 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 type of writing will be an error, if the cancellation of the global declaration of STR, will output AAA, that is, the function is still calling global variables.
Look at 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 the STR above, which writes "Str", which means that the timer calls the function directly as a parameter. The arguments passed in this way are always strings. This is not the result we want.

To pass a parameter that is unexpected except for a string, you can use the closure to 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 ', and if you put quotes to Auto (str), you will also get an error.
Of course, this is also good to write:

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 (); 

Finally, note that the word does not use the closure pass parameters, the timer called the function is to be quoted, without quotes, will be an error. The above situation is also suitable for setinterval ();


function calls in SetTimeout, setinterval
have the following code:

var num = 2; 
function Auto () { 
  alert (num); 
} 
settimeout (Auto (), 4000); 

In this program, the pop-up warning box is immediately visible at the time of the test. In other words, the timer does not work by referencing the function as described above.

Similarly, for the setinterval above the writing also does not work correctly, the program can only pop up once warning box, then the error.
Change the timer to

SetInterval (' Auto () ', 4000); 
SetTimeout (' Auto () ', 4000); 

The program can work correctly.


When you do not use the auto () call function, what is the only use of auto?

var str = ' AAA '; 
var num = 2; 
function Auto () { 
  alert (num); 
} 
SetInterval (auto,4000); 
SetTimeout (auto,4000); 

This writing program can work correctly;

If you enclose auto with quotes

SetInterval (' auto ', 4000); 
settimeout (' auto ', 4000); 

are not working properly.

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.