JavaScript timer and optimized cancellation timer method _javascript Tips

Source: Internet
Author: User
Tags setinterval

The usual method:
Start Timer:

Copy Code code as follows:

Window.setinterval (Method,time)

Method is a timed call of JS

Time is the interval, in milliseconds
Cancel Timer:

Copy Code code as follows:

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

Copy Code code as follows:

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 times, the unit millisecond
}

Summarize

Generally do not need to setinterval, and use settimeout delay to return instead of interval.
SetInterval will generate a callback backlog, especially when the time is short.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.