Setinterval/clearinterval and setTimeout/cleartimeout in JS

Source: Internet
Author: User

[SetTimeout]
SetTimeout (expression, latency)
During execution, an expression is executed after a specified time delay after loading,The number of times is once

[Setinterval]
Setinterval (expression, interaction time)
It is different. After it is loaded,Execute at specified timeOne expression

Because this is often used and I have to remember it completely every time, I always need to search the Internet once. It is too troublesome to write a test page by myself.

<HTML>
<Head>
<Title> setTimeout and setinterval test cases </title>
</Head>
<Body>
<Script language = "JavaScript">
// Set global variables
VaR staticobj = {};
Staticobj. Timer = "";
Staticobj. timer2 = "";
Staticobj. Count = 0;

Function startreload ()
{
// Set interval
Staticobj. Timer = setinterval ("reloaddata ()", 500 );
}

Function stopreload ()
{
// Clear Interval
Clearinterval (staticobj. Timer );
}

Function reloaddata ()
{
Staticobj. Count ++;
Alert ("interval cycles:" + staticobj. Count );
If (staticobj. Count = 5)
{
Stopreload ();
Alert ("the interval loop has ended! Start to enter the setTimeout function ");
// Set timeout
Staticobj. timer2 = setTimeout (timeoutfn, 100 );
}
}

Function timeoutfn ()
{
Cleartimeout (staticobj. timer2 );
Alert ("the setTimeout function is executed and the object is cleared ");
}

Startreload ();
</SCRIPT>
</Body>
</Html>

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.