Instructions for using JavaScript settimeout and setinterval _javascript tips

Source: Internet
Author: User
Tags setinterval javascript settimeout
The difference is that setinterval executes the code once every other specified time period, with repeatability. and settimeout only executes once after the call.
The following is a deep understanding of two functions through the establishment of functions and the automatic deletion of letters;
1. The establishment of the function
The establishment of settimeout:
Copy Code code as follows:

ShowTime ();
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
SetTimeout ("ShowTime ()", 5000);
}

The Showtime function is not performed once for five seconds after the function is called
The establishment of SetInterval
Copy Code code as follows:

SetInterval ("ShowTime ()", 5000);
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
}

Conclusion: Seemingly two functions have similar results, but the second function repeats the time until the page is closed.
Elimination of two functions:
Elimination of the use of settimeout
Cleartimeout () function; instance of the invocation:
Copy Code code as follows:

var timeoutprocess = settimeout ("alert (' goal! ')", 3000);
var stopgoallink = document.getElementById ("Stopgoallink");
Attacheventlistener (Stopgoallink, "click", Stopgoal, false);/join event function, parameter (target; event; called function; bubble)
function Stopgoal ()
{
Cleartimeout (timeoutprocess);
}

Elimination of SetInterval
Copy Code code as follows:

var timeoutprocess = settimeout ("alert (' goal! ')", 3000);
var stopgoallink = document.getElementById ("Stopgoallink");
Attacheventlistener (Stopgoallink, "click", Stopgoal, False), or join event function, parameter is (target; event; called function; bubbling)
function Stopgoal ()
{
Clearinterval (timeoutprocess);
}
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.