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