Timeout call setTimeout and intermittent call setInterval, settimeinterval

Source: Internet
Author: User

Timeout call setTimeout and intermittent call setInterval, settimeinterval
Today, I read the javascript advanced programming (the third edition) book and found that setTimeout is better than setInterval. SetInterval is usually used for multiple points. Now let's take a look. I learned it again. Note: parameters ------------ setTimeout contains two parameters. The first parameter is the code to be executed, and the second parameter is the time. The first parameter can be a string or a function, but we recommend that you use a function instead of a string. Use a string equivalent to the eval method. Performance Loss.
The clearTimeout () Timeout call code is executed in the global scope. Therefore, the value of this in the function points to the window object in the strict billing mode, and the value is undefined in the strict mode.

// SetInval

VarNum= 0;
VarMax= 10;
VarIntervalId=Null;

FunctionIncrementNumber(){
Num++;
If(Num=Max){
ClearInterval (innervalId );
Alert ('Done');
}
}

IntervalId= SetInterval (IncrementNumber() 500 );


// SetTimeoutImplement the same function

VarNum= 0;
VarMax= 10;
FunctionIncrementNumber2(){
Num++;
If(Num<Max){
SetTimeout (IncrementNumber2, 500 );
}Else{
Alert ('Done');
}
}
SetTimeout (IncrementNumber2, 500 );


The above comparison shows that there is no need to track the timeout call id when using a supermarket call, because after each code execution, if another timeout call is no longer set, the call will be stopped by itself. It is generally considered that a time-out call is an optimal mode to simulate intermittent calls. In the development environment, there are few real intermittent calls, because the latter intermittent call may be started before the previous call ends.
It is best not to use 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.