The usual method:
Start Timer:
[JavaScript]View PlainCopy
- Window.setinterval (Method,time)
Method is a timed call to JS methods
Time is the interval, in milliseconds
Cancel Timer:
[JavaScript]View PlainCopy
- Clearinterval (Method);
So here's the problem. With Clearinterval (Timerid), to clear, often can not stop immediately, with what method better solve?
The optimization scheme is as follows
[JavaScript]View PlainCopy
- var timeout = false; //Start and Close button
- function Time ()
- {
- if (timeout) return;
- Method ();
- SetTimeout (time,100); //time refers to itself, the delay recursively calls itself, 100 is the interval call time, unit milliseconds
- }
Summarize
Generally do not setinterval, and use settimeout delay to return to replace interval.
SetInterval will generate callback stacks, especially when the time is short.
JavaScript timer, cancellation timer, and JS Timer optimization method