JavaScript timer cancel timer and js timer optimization method
Common methods:
Start the timer:
(Method,Time)
Method is a timed call js method
Time is the interval time, in milliseconds
Cancel the timer:
clearInterval(Method);
Then the question is. Use clearInterval(timerid); to clear it, and it often cannot be stopped immediately. What method is easier to solve?
The optimization plan is as follows
var timeout = false; //Start and close buttonfunction time() { if(timeout) return; Method(); setTimeout(time,100); //time refers to itself, delayed recursive call itself, 100 is the interval call time, unit milliseconds}
Summarize
Generally, setInterval is not used, but delay recursion of setTimeout is used instead of interval.
setInterval will generate callback stacking, especially when the time is short.
Thank you for reading, I hope it can help you. Thank you for your support for this site!