Timeout calls and intermittent calls

Source: Internet
Author: User

1. First, it is clear that JavaScript is a single-threaded language and can only execute a piece of code over time. So there is a JavaScript task queue to control the order in which the code executes. the second parameter of the property settimeout () of the Window object is to tell JavaScript to add the current task to the queue for too long . If the queue is empty, the added code executes immediately, and if the queue is not empty, wait until the previous code executes before executing. Intermittent invocation of setinterval () is similar to a timeout call, except that it is called repeatedly.

SetTimeout (function() {      alert ("Hello");  },1000);

The first argument of SetTimeout () and setinterval () can be a function or a string, but it is not recommended to use a string, because code wrapping in a string requires two parsing to cause performance problems. Also causes scope problems

var name= "Liu";
function Sayname () {
var name= "Liuliu";

SetTimeout ("Alert (name)", 1000); //String form Liu

   }
Sayname ();

//////////////////////////////////////////////////

var name= "Liu";
function Sayname () {
var name= "Liuliu";

SetTimeout (function () {
alert (name);
},1000); //function form Liuliu
}
Sayname ();

2. Both the timeout call and the intermittent call return an identifier that can be used to cancel the call, Cleartimeout () and Clearinterval (). Usually only intermittent calls need to cancel the call operation, otherwise it will be repeated. In practice, a timeout call is typically used to simulate intermittent calls, because intermittent calls may be called before the end of the previous intermittent call.

 //Intermittent call  
var num=0, Max =5 =null ; function Incrementnumber () {num ++ if (Num==max) {clearinterval (Intervali D); // Cancel intermittent calls to Num=max alert (num); }} setinterval (Incrementnumber, 1000); // intermittent call

//Analog intermittent call with timeout call
var num=0,
max=5;
function Incrementnumber () {
num++;
if (Num<max) {
SetTimeout (arguments.callee,1000); //callback function, and the coupling relationship between function and function name is lifted
}else{
alert (num);
}
}
SetTimeout (incrementnumber,1000); //Timeout call

3. The code for the timeout call is executed in the global scope , so the function's Thisvalue points to the Window object in non-strict mode and is undefined in strict mode.

varName= "Liu"; varo={name:"Liuliu", Sayname:function() {alert ( This. name);//Liuliu       }   }; O.sayname (); the This value of the//timeout call varName= "Liu"; varo={name:"Liuliu", Sayname:function() {setTimeout (function(){//Timeout CallAlert This. name);//Liu},1000);   }   }; O.sayname ();

4. Two ways to call:

functionSayname () {alert ("Liu"); }       functionTest () {functionSayname () {alert ("Liuliu"); } setTimeout (Sayname, );   //Liuliu           setTimeout ("Sayname ()", +);  //Liu} test ();

//////////////////////////////////////////////////// functionSayname () {alert ("Liu"); } functionTest () {/*function Sayname () {alert ("Liuliu"); }*/setTimeout (Sayname,1000);//LiuSetTimeout ("Sayname ()", 1000);//Liu} test ();/////////////////////////////////////////////////////*function Sayname () {alert ("Liu"); }*/ functionTest () {functionSayname () {alert ("Liuliu"); } setTimeout (Sayname,1000);//LiuSetTimeout ("Sayname ()", 1000);//show sayname not defined} test ();////////////////////////////////////////////////////(function() { //Added a block-level scope functionSayname () {alert ("Liu"); } functionTest () {functionSayname () {alert ("Liuliu"); } setTimeout (Sayname,1000);//LiuliuSetTimeout ("Sayname ()", 1000);//show sayname not defined} test (); })();

setTimeout ("Sayname ()", 1000) This form, can only call functions in the global scope, I do not recommend this way. SetTimeout (sayname,1000) adheres to general scope access rules.

Timeout calls and intermittent calls

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.