SetTimeout and SetInterval detailed

Source: Internet
Author: User

SetTimeout and setinterval have the same syntax. They all have two parameters, one is the code string to execute, and one is the time interval in milliseconds, and the code executes after that time period.
However, there are differences between the two functions:

SetInterval after executing the code once, after that fixed time interval, it also automatically repeats the code, and settimeout only executes the code once.
Difference:
Window.settimeout ("function", time);//Set a timeout object, execute once, no cycle
Window.setinterval ("function", time);//Set a timeout object, period = ' 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

Perrefresh (); function Perrefresh () {     varnew  Date ();     Alert ("The time is:" + today.tostring ());     SetTimeout ("ShowTime ()", "the");}

Once the function Perreflesh is called, the time is displayed every 5 seconds

SetInterval ("Perrefresh ()", "the"); function Perrefresh () {     varnew  Date ();     Alert ("The time is:" + today.tostring ());}

But setinterval is not bound by the function that he calls, it simply repeats the function once every time.
Whenever SetInterval ("Perrefresh ()", 5000) of this function is called, the function Perrefresh is executed every 5 seconds.
If it is required to perform an action precisely after every fixed interval, it is best to use setinterval, and if you do not want to cause mutual interference with successive calls, especially if the call of a function requires heavy computation and long processing time, then it is best to use settimeout.
SetInterval continuously executes the specified code until it calls Clearinterval to clear the timer object
SetTimeout executes the specified code once and clears the timer object with Cleartimeout
Both SetInterval and settimeout return timer object identifiers for Clearinterval and Cleartimeout calls

SetTimeout and SetInterval detailed

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.