Analysis of JavaScript interval call and Delay call _ basic knowledge

Source: Internet
Author: User
Tags setinterval

The SetInterval method can be used to implement circular call functions at specified intervals until the Clearinterval method cancels the loop

When you cancel a loop with the Clearinterval method, you must assign the call to the SetInterval method to a variable, and then the Clearinterval method references the variable.

Copy Code code as follows:

<script type= "Text/javascript" >
var n = 0;
function print () {
Document.writeln (n);

if (n==1000) {
Window.clearinterval (s);
}
n++;
}
var s = window.setinterval (print, 10);
</script>

Completes the deferred call with SetTimeout and cleartimeout, runs the specified function after the specified delay time, and executes only once. The use of cleartimeout is the same as that of the Clearinterval method.

Copy Code code as follows:

<script type= "Text/javascript" >
function Printtime () {
var time = new Date ();
var year = Time.getfullyear ();
var month = (Time.getmonth ()) +1;
var daynum = Time.getday ();
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
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>

Related Article

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.