Regular JavaScript call functions (setinterval and setTimeout)

Source: Internet
Author: User

the setTimeout and setinterval syntaxes are the same. They both have two parameters: one is the Code string to be executed, and the other is the interval in milliseconds, after that time period, the code will be executed.

however, the two functions are different. After executing the code, setinterval passes through the fixed interval, it also automatically repeats the code, while setTimeout only executes that code once.

difference: window. setTimeout ("function", time); // sets a time-out object, which is executed only once, no cycle
window. setinterval ("function", time); // you can specify a timeout object. The period is set to 'interaction time'.

Stop timing: 
Window. cleartimeout (object) Clear the setTimeout object that has been set
Window. clearinterval (object) Clear the setinterval object that has been set

 
Perrefresh (); function perrefresh () {var today = new date (); alert ("the time is:" + today. tostring (); setTimeout ("Showtime ()", 5000 );}

 Once this function is called, the time is displayed every five seconds.

 
Setinterval ("perrefresh ()", 5000); function perrefresh () {var today = new date (); alert ("the time is:" + today. tostring ());}

while setinterval is not bound by the function called by itself, it simply repeats the function once every certain time.

If the setinterval (" perrefresh () ", 5000) function is called, The perrefresh function is executed every five seconds.

if you want to execute an action accurately after a fixed interval, it is best to use setinterval, if you don't want to interfere with each other due to continuous calls, especially if each function call requires heavy computing and a long processing time, you 'd better use setTimeout.

Setinterval continues to execute the Specified Code until clearinterval is called to clear the timer object

SetTimeout: run the specified code once and clear the timer object using cleartimeout.

Both setinterval and setTimeout return the timer object identifier for clearinterval and cleartimeout calls.

 

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.