Usage of setinterval and settimeout event stops in JS

Source: Internet
Author: User
Tags setinterval time interval


SetInterval () Definition and usage

The SetInterval () method executes a function or expression in the specified period (in milliseconds). The method loops through the function until the function is explicitly stopped by using clearinterval (), or the window is closed. The parameter of the Clearinterval () function is the ID value returned by SetInterval ().

Grammar

SetInterval (code,millisec[, "Lang"])

Code required. The function to invoke or the code string to execute.

Millisec must. The time interval, in milliseconds, between the periodic execution or invocation of code.

return value

A value that can be passed to Window.clearinterval () to cancel the periodic execution of code


(1) The SetInterval method can call a function or evaluate an expression according to the specified period (in milliseconds) and stop the method using the Clearinterval method. The specific examples are as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<input type= "text" id= "clock" size= "/>"
<script language=javascript>
var int=self.setinterval ("Clock ()", 50);//Call clock () function every 50 milliseconds
function Clock () {
var t=new Date ();
document.getElementById ("Clock"). value=t;
}
</script>
<button onclick= "window.clearinterval (int)" > Stop interval</button>
</body>

settimeout () Definition and usage

The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds, unlike the SetInterval () method, which is executed only once.

Grammar

SetTimeout (CODE,MILLISEC)

Code required. The JavaScript code string to execute after the function to be invoked.

Millisec required. The number of milliseconds to wait before executing the code, in milliseconds.

Tips:

(1) settimeout () although it is only executed once code. However, if you make multiple calls, you can use SetInterval () to call the SetTimeout () method again in the executed code for multiple execution purposes.

(2) Alternatively, the settimeout () method can also return an ID value to facilitate the cancellation of the use of the settimeout () method using the Clearinterval () method.


The Clearinterval () method cancels the parameter of the Timeout;clearinterval () method set by SetInterval () must be the ID value returned by SetInterval ().

(2) The SetTimeout method is used to call a function or evaluate an expression after a specified number of milliseconds. Stop this method to use the Cleartimeout method. The specific examples are as follows:

Tip: settimeout () executes only one code at a time. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again.

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" >
var c=0;
var T;
function Timedcount () {
document.getElementById (' txt '). value=c;
c=c+1;
T=settimeout ("Timedcount ()", 1000);
}
function Stopcount () {
Cleartimeout (t);
}
</script>
<body>
<input type= "button" value= "Start Count" onclick= "Timedcount ()" >
<input type= "text" id= "TXT" >
<input type= "button" value= "Stop Count" onclick= "Stopcount ()" >
</body>


The Cleartimeout () method cancels the timeout set by the settimeout () method.

Grammar cleartimeout (id_of_settimeout)

The parameter id_of_setinterval represents the ID value returned by settimeout (). This value identifies the block of deferred execution code to be canceled

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.