JavaScript interval calls and deferred calls examples

Source: Internet
Author: User

  This article mainly introduced the JavaScript interval call and the delay call example, introduced the SetInterval method and the Clearinterval method use method, everybody reference uses the bar

The SetInterval method enables circular call functions to be implemented at specified intervals until the Clearinterval method cancels the loop   cancels the loop with the Clearinterval method, the call to the SetInterval method must be assigned to a variable, The Clearinterval method then references the variable.     Code as follows: <script type= "Text/javascript" >     var n = 0;     function print () {        Document.writeln (n);           if (n== 1000) {        Window.clearinterval (s);        }         &NBS P   n++;    }      var s = window.setinterval (print, 10); </script>     Use settimeout and cleartimeout to complete the deferred call, run the specified function after the specified delay time, and execute only once. The use of cleartimeout is the same as that of the Clearinterval method.     Code as follows: <script type= "Text/javascript" >     function printtime () {        V Ar time = new Date ();         var year = Time.getfullyear ();         var month = (Time.getmonth ()) +1;         var daynum = Time.getday ();   &nbsp     var hour = time.gethours ();         var min = time.getminutes ();         var sec = Time.getseconds ();         var da = time.getdate ();         var daystr;         switch (daynum) {        case 0:daystr= "Sunday";             break;         case 1:daystr= "Monday";             break;         case 2:daystr= "Tuesday";             break;         case 3:daystr= "Wednesday";             break;         case 4:daystr= "Thursday";             break;         case 5:daystr= "Friday";             break;         case 6:daystr= "Saturday";             break;   &nbsp     default:daystr= "";        }         var str = year+ "year" +month+ "month" +da+ "Day  " +daystr+ "" +hour+ ":" + min+ ":" +SEC;         document.getElementById ("T"). InnerHTML = str;         Window.settimeout (printtime, 1000);    } </script>   <body onload= "Printtime ()" > <br/> <div id= "T" ></div> < /body>  

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.