Timeout call settimeout and intermittent call setinterval

Source: Internet
Author: User

Read the JavaScript Advanced Programming (third Edition) today, and found that settimeout is better than setinterval, and feels that it does. Usually use setinterval more points, now or turn the idea. I learned it again. The notes are as follows:-------------------------------------------------------------------------------------------------------------------- -----------------------settimeout contains two parameters, the first parameter is the code to execute, and the second parameter is the time. The first argument can be a string or a function, but it is recommended to use a function instead of a string. Use a string equivalent to the Eval method. Cause a loss of performance.
The code for the Cleartimeout () Timeout call is executed in the global scope, so the value of this in the function points to the window object in strict mode, which is undefined

 //setinval

varNum=0;
varMax=Ten;
varIntervalid=NULL;

functionIncrementnumber(){
Num++;
if(Num==Max){
clearinterval(Innervalid);
Alert(' Done ');
}
}

Intervalid=setinterval(Incrementnumber(), -);


//settimeout Achieve the same functionality

varNum=0;
varMax=Ten;
functionIncrementNumber2(){
Num++;
if(Num<Max){
SetTimeout(IncrementNumber2, -);
}Else{
Alert(' Done ');
}
}
setTimeout(incrementNumber2,);


The comparison shows that when using a supermarket call, it is not necessary to trace the timeout call ID, because after each execution of the code, the call stops itself if another timeout call is no longer set. It is generally considered that if the timeout call is used to simulate intermittent calls, it is an optimal mode. in the development environment, there are few real intermittent calls because the latter intermittent call may start before the end of the previous gap call.
It is best not to use intermittent calls.

Timeout call settimeout and intermittent call setinterval

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.