SetInterval () and Clearinterval (), settimeout (), and cleartimeout () __html in the HTML Dom

Source: Internet
Author: User
Tags setinterval
HTML DOM setinterval () and Clearinterval () methods

The setinterval () method can call a function or a calculation expression in the specified period (in milliseconds). The SetInterval () method keeps calling the function until the clearinterval () is called or the window is closed. The ID value returned by SetInterval () can be used as an argument to the Clearinterval () method.

The clearinterval () method cancels the timeout set by SetInterval (). The parameter of the Clearinterval () method must be the ID value returned by SetInterval ().

Case one: Implement a timer, require
1, from start to end (including start and last), every 100 milliseconds console.log a number, each number increase to 1
2, the returned object needs to include a Cancel method to stop the timed operation
3. The first number needs immediate output

function count (start, end) {
    if (start <=end) {
        console.log (start++);
        Xin=settimeout (function () {
            count (start,end);
        },100);
    return {
        cancel:function () {
            cleartimeout (xin);
        }
    } 
}
HTML DOM setinterval () and Clearinterval () methods

The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.
The cleartimeout () method cancels the timeout set by the settimeout () method.

Case two: A timer with a stop button

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.