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.
1 setTimeout (function (){
2 alert ("Hello! ");
3}, 1000 );
Cancel clearTimeout () before calling and accept a parameter timeout call ID
1 var timeOutId = setTimeout (function (){
2 alert ("Hello! ");
3}, 1000 );
4
5 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.
1 setInterval (function (){
2 alert ("Hello! ");
3}, 1000 );
Cancel call clearInterval () and accept a parameter's intermittent call ID
1 var intervalId = null;
2 var span = document. createElement ("span"); // create a span Node
3 span. Id = "time"; // you can specify the span id.
4 document. body. appendChild (span); // Add span to the body
5 function incrementNumber (){
6 var now = new Date ();
7 var timeStr = now. toLocaleTimeString ();
8 span. innerText = timeStr;
9 num ++;
10 if (num = 10 ){
11 clearInterval (intervalId); // The time remains unchanged after 10 seconds
12}
13}
14 intervalId = setInterval (incrementNumber, 1000 );
3. Try to replace intermittent calls with time-out calls
1 var num = 0;
2 var max = 10;
3 function incrementNumber (){
4 num ++;
5 if (num <max ){
6. setTimeout (incrementNumber, 1000 );
7} else {
8 alert ("OK ");
9}
10}
11 setTimeout (incrementNumber, 1000 );
From Sunday walk