SetInterval and SetTimeout delay method in JS

Source: Internet
Author: User
Tags manual setinterval

JS manual»settimeout (): Used to call a function or evaluate an expression after a specified number of milliseconds;
English explanation»timeout (): timeout, temporary rest, breaks;
JS manual»setinterval (): Invokes a function or evaluation expression in the specified period (in milliseconds). The function is called continuously until the Clearinterval () is called or the window is closed;
English explanation»interval (): interval; spacing; intermission;
It is not difficult to see, as long as we carefully understand the JS manual and the name of interpretation, can be very easy to separate the difference between the two. To put it simply, the difference between the two is that the settimeout () method executes the function after waiting for a specified time and executes only one incoming handle function. The SetInterval () method executes an incoming handle function after each specified interval, looping until the window or clearinterval () is closed.


Using setinterval and setting the delay function settimeout very similar. SetTimeout is used to delay a period of time and then perform an operation.

The code is as follows Copy Code

SetTimeout ("function", time) sets a timeout object
SetInterval ("function", time) sets a timeout object

SetInterval for automatic repetition, settimeout will not repeat.


Cleartimeout (object) clears the SetTimeout object that has been set
Clearinterval (object) clears the SetInterval object that has been set

If you implement the function of Setinerval with settimeout, you need to call yourself in the program you are executing. If you want to clear the counter you need to invoke different cleanup methods depending on the method you are using:

For example:

The code is as follows Copy Code

Tttt=settimeout (' Northsnow () ', 1000);

Cleartimeout (TTTT);

Or:

Tttt=setinterval (' Northsnow () ', 1000);

Clearinteval (TTTT);

Give an example:

  code is as follows copy code


<div id= "Liujincai" ></div>
<input type= "button" name= "Start" value= "Start" onclick= ' Startshow ' (); ' >
<input type= "button" name= "Stop" value= "Stop" onclick= "Stop ();" >
<script language= "JavaScript" >
var intvalue=1;
var timer2=null;
function Startshow ()
{
Liujincai.innerhtml=liujincai.innerhtml + "" + (Intvalue + +). toString ();
Timer2=window.settimeout ("Startshow ()", 2000);
}
function Stop ()
{
Window.cleartimeout (TIMER2);
}
</script>


Or:

The code is as follows Copy Code

<div id= "Liujincai" ></div>
<input type= "button" name= "Start" value= "Start" onclick= ' Timer2=window.setinterval ("startshow ()", 2000); Startshow (); ' >
<input type= "button" name= "Stop" value= "Stop" onclick= "Stop ();" >
<script language= "JavaScript" >
var intvalue=1;
var timer2=null;
function Startshow ()
{
Liujincai.innerhtml=liujincai.innerhtml + "" + (Intvalue + +). toString ();
}
function Stop ()
{
Window.clearinterval (TIMER2);
}
</script>

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.