1. Timeout call settimeout ()
The settimeout () method accepts two parameters, the first parameter is a function, the second parameter is the time (in microseconds), and returns the value ID
Copy Code code as follows:
settimeout (function () {
Alert ("Hello!");
},1000);
Cancels cleartimeout () before calling to accept a parameter timeout call ID
Copy Code code as follows:
var Timeoutid = settimeout (function () {
Alert ("Hello!");
},1000);
Cleartimeout (Timeoutid);
2. Intermittent call SetInterval ()
The SetInterval () method accepts two parameters, the first parameter is a function, the second parameter is the time (in microseconds), and returns the value ID
Copy Code code as follows:
SetInterval (function () {
Alert ("Hello!");
},1000);
Cancels calling Clearinterval (), accepting a parameter intermittent call ID
Copy Code code as follows:
var intervalid = null;
var span = document.createelement ("span"); To create a span node
Span. Id= "Time"; Set the ID of span
Document.body.appendChild (span); Add span within body
function Incrementnumber () {
var now = new Date ();
var timestr = now.tolocaletimestring ();
Span.innertext =timestr;
num++;
if (num = 10) {
Clearinterval (Intervalid); In 10 seconds, the time is unchanged.
}
}
Intervalid = SetInterval (incrementnumber,1000);
3. Try to use timeout call instead of intermittent call
Copy Code code as follows:
var num = 0;
var max = 10;
Function Incrementnumber () {
num++;
if (num<max) {
settimeout (incrementnumber,1000);
} else {
alert ("OK");
}
}
settimeout (incrementnumber,1000);