Regular JavaScript call functions (SetInterval and setTimeout) and javascript call Functions

Source: Internet
Author: User

Regular JavaScript call functions (SetInterval and setTimeout) and javascript call Functions

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

However, there are differences between the two functions. After the setInterval code is executed, it automatically repeats the code after the fixed interval, setTimeout only executes the code once.

Difference: window. setTimeout ("function", time); // you can specify a time-out object, which is executed only once without a period.
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 at a certain time.

If setInterval ("PerRefresh ()", 5000) is called, The PerRefresh function is executed every five seconds.

If you want to execute an action accurately after a fixed interval, you 'd better use setInterval. If you don't want to interfere with each other due to continuous calls, in particular, each function call requires heavy computing and a long processing time, so it is best to 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.


The following is a small example:

<Html>
<Head>
<Script type = "text/javascript">
Var I = 0;
Var;
Function test (){
A = setInterval ("showTime ()", 5000 );
}
Function showTime (){
Var today = new Date ();
Alert ("the time is" + today. toString ());
I ++;
Alert (I );
If (I = 2 ){
ClearInterval (a); // The clearing timer needs to pass in an object (the object you want to clear)
}
}
</Script>
</Head>
<Body> my first js setInterval and setTimeout function
<Input type = "button" value = "setinterval" onclick = "test ();"> </body>
</Html>








How to write the test () function regularly called in JavaScript?

SetTimeout ("test ()", 1000): executed once after a given time
SetInterval ("test ()", 1000)
The time unit is milliseconds.
SetTimeout can also be used for repeated execution after a fixed period of time:
Function test (){
// Your logic processing
SetTimeout ("test ()", 1000 );
}
The difference between the two methods is:
SetTimeout must be executed after your logic is executed at a fixed time. It is a single process and does not affect public data.
SetInterval is executed every fixed time, regardless of whether the previous method has been completed or not. It is a multi-process, and an error may occur when you modify the total data.

Question about the setTimeout Function in js?

They are all executed after the execution is completed. You can try to run this Code:
SetInterval (function () {alert ('only one 'will appear at a time) ;}, 3000); setTimeout is executed once after the delay. While setInterval is always delayed and repetitive.


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.