js-timed Task Setinterval,settimeout,clearinterval,cleartimeout

Source: Internet
Author: User

SetInterval () loop executes the appropriate method

<script type= "Text/javascript" >      setinterval ("Myinterval ()", +);   1000 for 1 seconds       function  myinterval ()       {            alert (' aaaaaaaaa ');         </script>

SetTimeout () to be executed after the specified time

1. SetTimeout (Expression,delaytime), after Delaytime, will perform a expression,settimeout application after a delay for a period of time, and then perform an operation. SetTimeout ("", Time) sets a timeout object 2. SetInterval (Expression,delaytime), each delaytime, Will execute expression. Often used to refresh expressions. SetInterval ("", Time) sets a timeout object setinterval to Auto-Repeat, settimeout does not repeat. 3. 1clearTimeout (object) clears the set SetTimeout object 3.2clearInterval (object) clears the SetInterval object that has been set

Use of settimeout and setinterval

Both of these methods can be used to implement JavaScript after a fixed time period. But each has its own application scenario.

Method

SetTimeout and setinterval have the same syntax. They all have two parameters, one is the code string to execute, and one is the time interval in milliseconds, and the code executes after that time period.

However, the two functions are still different, setinterval after the execution of the code, after the fixed time interval, it will also automatically repeat the code, and settimeout only one time to execute the code.

Although it appears that settimeout can only be applied to actions in On-off mode, it is possible to repeatedly invoke settimeout by creating a function loop for repeated operations:

showTime (); function ShowTime () {    varnew  Date ();    Alert ("The time is:" + today.tostring ());    SetTimeout ("ShowTime ()", "the");}

Once this function is called, the time is displayed every 5 seconds. If you use SetInterval, the corresponding code is as follows:

SetInterval ("ShowTime ()", "the"); function ShowTime () {    varnew  Date ();    Alert ("The time is:" + today.tostring ());}

The two methods may look very similar, and the results shown will be much the same, but the big difference is that the SetTimeout method does not execute the Showtime function every 5 seconds, and it executes the Showtime function 5 seconds after each call to settimeout. This means that if the body part of the Showtime function takes 2 seconds to execute, the entire function is executed every 7 seconds. But setinterval is not bound by the function that he calls, it simply repeats the function once every time.

If it is required to perform an action precisely after every fixed interval, it is best to use setinterval, and if you do not want to cause mutual interference with successive calls, especially if the call of a function requires heavy computation and long processing time, then it is best to use settimeout.

Discussion

If the timer function is not processed, then SetInterval will continue to execute the same code until the browser window closes, or the user goes to another page. However, there are ways to terminate the execution of the settimeout and setinterval functions.

When the setinterval call executes, it returns a timer ID that can be used to access the timer in the future and, if passed to Clearinterval, terminates the execution of the called procedure Code, implemented as follows:

var = setinterval ("alert (' goal! ')", intervalprocess); var stopgoallink = document.getElementById ("Stopgoallink"false); function Stopgoal () {   clearinterval (intervalprocess);}

As long as you click on the Stopgoallink, no matter when you click, Intervalprocess will be canceled, and will not continue to execute intervalprocess again and again.

If the settimeout is canceled within the time-out period, the termination effect can also be implemented in SetTimeout, as follows:

var = setTimeout ("alert (' goal! ')", timeoutprocess); var stopgoallink = document.getElementById ("Stopgoallink"false); function Stopgoal () {   cleartimeout (timeoutprocess);}

10 seconds auto Jump case

<Divclass= "Jf_register">    <H2>Hello, Welcome to visit<Fmt:messageKey= "B2cshowname"/>!<aclass= "Blue"href= "<%=path%>/loginregist_turntologin">Please login</a></H2></Div><Divclass= "Jf_register_banner">Registration successful</Div><Divclass= "Jf_password"><ul><Li>Thank you for choosing<Fmt:messageKey= "B2cshowname"/>,</Li><Li>Your account<span><FontColor=red><Strong>${PARAM.BUSSINESSID}</Strong></Font></span>has registered successfully.</Li><Li>The system will be<StrongID= "Endtime"></Strong>seconds to jump to the login page! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<aclass= "Blue"href= "<%=path%>/loginregist_turntologin">Direct Login</a></Li></ul></Div><Divclass= "Jf_loginfooter"><Fmt:messageKey= "Copyright"/>&nbsp;All rights reserved&nbsp;| <ahref= "Login_loginhelptips">Contact Us</a><Fmt:messageKey= "COPYRIGHTICP"/></Div><Scripttype= "Text/javascript">varI= Ten;functionRemaintime () {if(i==0) {Location.href='<%=path%>'; } document.getElementById ('Endtime'). InnerHTML=I--; SetTimeout ("remaintime ()", +);} Remaintime ();</Script>

js-timed Task Setinterval,settimeout,clearinterval,cleartimeout

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.