1. Timeout call setTimeout ()
The setTimeout () method accepts two parameters. The first parameter is the function, and the second parameter is the time (in microseconds). The value ID is returned.
Copy codeThe Code is as follows:
SetTimeout (function (){
Alert ("Hello! ");
},1000 );
Cancel clearTimeout () before calling and accept a parameter timeout call ID
Copy codeThe Code is as follows:
Var timeOutId = setTimeout (function (){
Alert ("Hello! ");
},1000 );
ClearTimeout (timeOutId );
2. Call setInterval () intermittently ()
The setInterval () method accepts two parameters. The first parameter is the function, and the second parameter is the time (in microseconds). The value ID is returned.
Copy codeThe Code is as follows:
SetInterval (function (){
Alert ("Hello! ");
},1000 );
Cancel call clearInterval () and accept a parameter's intermittent call ID
Copy codeThe Code is as follows:
Var intervalId = null;
Var span = document. createElement ("span"); // create a span Node
Span. Id = "time"; // you can specify the span id.
Document. body. appendChild (span); // Add span to the body
Function incrementNumber (){
Var now = new Date ();
Var timeStr = now. toLocaleTimeString ();
Span. innerText = timeStr;
Num ++;
If (num = 10 ){
ClearInterval (intervalId); // The time remains unchanged after 10 seconds
}
}
IntervalId = setInterval (incrementNumber, 1000 );
3. Try to replace intermittent calls with time-out calls
Copy codeThe Code is as follows:
Var num = 0;
Var max = 10;
Function incrementNumber (){
Num ++;
If (num <max ){
SetTimeout (incrementNumber, 1000 );
} Else {
Alert ("OK ");
}
}
SetTimeout (incrementNumber, 1000 );