Reprinted from: http://blog.csdn.net/johnstrive/article/details/42216005
Commonly used method:
Start the timer:
- window.setInterval(Method,Time)
Method is a js method that is called regularly
Time is the interval time in milliseconds
Cancel the timer:
- clearInterval(Method);
So here comes the problem. Use clearInterval(timerid); to clear, often can not stop immediately, what method is better to solve?
The optimization scheme is as follows
- var timeout = false ; // start and close buttons
- function time()
- {
- if(timeout) return;
- Method();
- setTimeout(time,100); //time refers to itself, delay recursive call to itself, 100 is the interval call time, in milliseconds
- }
Summary
Generally, setInterval is not used, but the delay recursion of setTimeout is used instead of interval.
setInterval can create a stack of callbacks, especially when the time is short.