Difference between setTimeout and setinterval

Source: Internet
Author: User

Both setTimeout and setintelval have the timer function !!! When the scheduled task is canceled, cleartimeout and clearinterval correspond to each other.

But there is a difference between them!

SetTimeout indicates that the task is executed only once after a period of time.

Setinterval indicates that the execution is performed multiple times at intervals.

 

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> new document </title> <br/> <meta name = "generator" content = "editplus"> <br/> <meta name = "author" content = ""> <br/> <meta name = "keywords" content = ""> <br/> <meta name = "Description" content = ""> <br/> </pead> </P> <p> <body> <br/> <Div id = "time"> </div> <br/> <script language = "JavaScript"> <br/> <! -- <Br/> var timeid; <br/> function settime () {<br/> var date = new date (); <br/> var H = date. gethours (); <br/> var M = date. getminutes (); <br/> var S = date. getseconds (); <br/> document. getelementbyid ("time "). innerhtml = H + ":" + M + ":" + S; <br/>}< br/> timeid = setinterval ("settime ()", 1000 ); // Its call is an external call, instead of a recursive call. It indicates that the call is performed once every 1 S. <br/> function stop () {<br/> clearinterval (timeid ); <br/>}< br/> // --> <br/> </SCRIPT> <br/> <input type = "button" value = "Suspend" onclick =" stop () "> <br/> <input type =" button "value =" start "onclick =" timeid = setinterval ('settime () ', 1000 ); "> <br/> </body> <br/> </ptml> </P> <p>

SetTimeout

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> new document </title> <br/> <meta name = "generator" content = "editplus"> <br/> <meta name = "author" content = ""> <br/> <meta name = "keywords" content = ""> <br/> <meta name = "Description" content = ""> <br/> </pead> </P> <p> <body> <br/> <Div id = "time"> </div> <br/> <script language = "JavaScript"> <br/> <! -- <Br/> var timeid; <br/> function settime () {<br/> var date = new date (); <br/> var H = date. gethours (); <br/> var M = date. getminutes (); <br/> var S = date. getseconds (); <br/> document. getelementbyid ("time "). innerhtml = H + ":" + M + ":" + S; <br/> timeid = setTimeout ("settime ()", 1000); // recursive call, 1 s later <br/>}< br/> settime (); // start execution after page loading <br/> function stop () {<br/> cleartimeout (timeid ); <br/>}< br/> // --> <br/> </SCRIPT> <br/> <input type = "button" value = "Suspend" onclick =" stop () "> <br/> <input type =" button "value =" start "onclick =" timeid = setinterval ('settime () ', 1000 ); "> <br/> </body> <br/> </ptml> <br/>

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.