Timers in JavaScript

Source: Internet
Author: User

In JAVASCRITP, there are two dedicated functions for timers, respectively:

1. Countdown Timer: Timename=settimeout ("function ();", delaytime); 2. Cycle Timer: Timename=setinterval ("function ();", delaytime);

The first parameter, "function ()", is the action to be performed when the timer is triggered, either as a function or as a function, separated by a ";" between functions. For example, to eject two warning windows, you can "function ();" Change to "alert (' First warning window! '); Alert (' Second warning window! '); "   The second parameter, "Delaytime", is the time interval, in milliseconds, that is, "5000", which represents 5 seconds. The countdown timer is the trigger event at the specified time, and the loop timer is the recurring event when the interval arrives, the difference being that the former is only a function once, while the latter is continuously functioning. For example, when you open a page and want to automatically jump to another page in a few seconds, you need to use the SetTimeout timer "(" function (), ", Delaytime)", and if you want to set a sentence to a single word, you need to use the loop timer " SetInterval ("function ();", Delaytime) ".

Gets the focus of the form, using the document.activeElement.id. Use if to determine if document.activeElement.id and form IDs are the same. For example: if ("mid" = = Document.activeElement.id) {alert ()}, "Mid" is the ID of the form.

Timer: Used to specify that a program is executed after a specified period of time.

JS in the timing of execution, settimeout and setinterval differences, and L lifting method

SetTimeout (Expression,delaytime), after Delaytime, will perform a expression,settimeout application for a period of delay before performing an operation. SetTimeout ("function", time) sets a timeout object

SetInterval (Expression,delaytime), each delaytime, will execute expression. It is often used to refresh an expression. SetInterval ("function", time) sets a timeout object

SetInterval for automatic repetition, settimeout does not repeat.

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

Give a brief account of two cases. Example 1. Verbatim output strings when a form is triggered or loaded

Copy CodeThe code is as follows:


Example 2. When the focus is on the input box, check the input box information periodically, and do not perform the check action when the focus is absent.

Copy CodeThe code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Example 3: The following is the simplest example, the timer time arrives after the warning window pops up.

Copy CodeThe code is as follows:

Example 4: Countdown timer jump

Copy CodeThe code is as follows:



Example 6:

Copy CodeThe code is as follows:<meta http-equiv= "Refresh" content= "2;url= ' b.html '" >

Example 7:

Copy CodeThe code is as follows: <script language= "JavaScript" type= "Text/javascript" > SetTimeout ("window.location.href= ' b.html '", 2000)  ;  The following two can be used//settimeout ("javascript:location.href= ' b.html '", 2000); SetTimeout ("window.location= ' b.html '", 2000); </script>

Example 8:

Copy CodeThe code is as follows: <span id= "Totalsecond" >2</span> <script language= "javascript" type= "Text/javascript" > var  Second = document.getelementbyidx_x (' Totalsecond '). InnerHTML; if (IsNaN (second)) {//...    Not the number of processing methods}else{setinterval (function () {document.getelementbyidx_x (' Totalsecond '). InnerHTML =--second;    if (second <= 0) {window.location = ' b.html ';  }}, 1000); }</script>

JS Timer (execute once, repeat)

Share a JS code, a small example of the JS timer, divided into the execution of a timer and repeated execution of the timer. For beginners of friends reference.

1, the timer only executes once

Copy CodeThe code is as follows:<script>the//timer asynchronously runs function hello () { alert ("Hello"); } //Use method name execution method var t1 = window.settimeout (hello,1000); var t2 = window.settimeout ("Hello ()", 3000);//Use String execution method window.cleartimeout (t1);//Remove Timer </script>< /c8>

2, repeat the timer

Copy CodeThe code is as follows:<script>function Hello () { alert ("Hello"); } //Execute a method repeatedly var t1 = window.setinterval (hello,1000); var t2 = window.setinterval ("Hello ()", 3000); //The method of removing the timer window.clearinterval (t1); </script>

Note:

If there are two methods on a page that are executed after the page has been loaded, the actual execution is not sequential and can be resolved as follows: You can add a timer to the OnLoad method, set a timer, and then run after a period of time, can be considered to distinguish the order of the page load Run method.

Copy CodeThe code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script language= "javascript" type= "Text/javascript" > var YC = new Object ();         function Beginyc () {var secondsyc = document.getElementById ("Txtycseconds"). Value; try {YC = SetTimeout ("alert (' Delay" +secondsyc+ "second Success ')", secondsyc*1000); } catch (e) {alert ("Please enter the correct number of seconds.")         ");         }} function Overyc () {cleartimeout (YC);         Yc=null; Alert ("Termination delay succeeded.     "); }
Use of the/**************************↓↓↓↓ timer ↓↓↓↓********************************/
    var timerds = new Object ();     var timerdds = new Object ();     function Beginds ()     {        sn.innerhtml = " 0 ";         timerds = SetInterval ("AddOne ()", parseint (document.getElementById (" Txtintervalseconds "). value,10) *1000);    }     function Goonds ()     {         Timerds = SetInterval ("AddOne ()", parseint (document.getElementById ("Txtintervalseconds"). value,10) *1000);    }     function Overds ()     {         Clearinterval (timerds);         Timerds=null;    }     function delayds ()     {      & nbsp Overds ();         Timerdds = setTimeout ("Goonds() ", document.getElementById (" Txtddsseconds "). value*1000);    }     function AddOne ()     {         if (sn.innerhtml== ")         {             overds ();             alert ("Congratulations, you've reached 10 seconds");             return;        }         Sn.innerhtml=parseint ( sn.innerhtml,10) +1;    }
</script>

<label id= "DS" title= "Delay seconds:" ></label> <input type= "text" id= "Txtddsseconds" value= "5"/> <input type= "button" id= "Btndds" onclick= "javascript:delayds ()" value= "Delay Timer"/>
</div> </form> </body>

Timers in JavaScript

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.