JavaScript setinterval and settimeout using a detailed knowledge of the basics _

Source: Internet
Author: User
Tags setinterval time interval
SetTimeout and setinterval have the same syntax. They all have two parameters, one is the code string that will be executed, and one is the time interval in milliseconds, and the code will be executed after that time period.
However, these two functions are different, setinterval after the execution of the code, after the fixed time interval, it will also automatically repeat the code, and settimeout only one time to execute the code.
Difference:
Window.settimeout ("function", time);/Set a timeout object, only once, no cycles
Window.setinterval ("function", time);/Set a timeout object, cycle = ' interaction times '
Stop timing:
Window.cleartimeout (object) clears the SetTimeout object that has been set
Window.clearinterval (object) clears the SetInterval object that has been set
Copy Code code as follows:

Perrefresh ();

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

Once this function is called, the Perreflesh is displayed every 5 seconds.
Copy Code code as follows:

SetInterval ("Perrefresh ()", 5000);

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

But setinterval is not bound by the function it calls, it simply repeats the function once at a certain time.
As long as the SetInterval ("Perrefresh ()", 5000) is invoked, the function is executed every 5 seconds perrefresh.
If it is required to perform an action precisely after every fixed interval of time, it is best to use setinterval, and if you do not want to cause interference problems due to successive calls, especially if the call to each function requires heavy computation and lengthy processing time, it is best to use settimeout.
SetInterval continuously executes the specified code until the call Clearinterval clears the timer object
SetTimeout executes the specified code once, using Cleartimeout to clear the timer object
SetInterval and SetTimeout Both return timer object identifiers for Clearinterval and Cleartimeout calls
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.