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.