Introduction to js intermittent call and timeout call in javascript learning notes. For details about js learning, refer to section 1. setTimeout () for timeout call ()
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.
The Code is as follows:
SetTimeout (function (){
Alert ("Hello! ");
},1000 );
Cancel clearTimeout () before calling and accept a parameter timeout call ID
The 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.
The Code is as follows:
SetInterval (function (){
Alert ("Hello! ");
},1000 );
Cancel call clearInterval () and accept a parameter's intermittent call ID
The 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
The Code is as follows:
Var num = 0;
Var max = 10;
Function incrementNumber (){
Num ++;
If (num SetTimeout (incrementNumber, 1000 );
} Else {
Alert ("OK ");
}
}
SetTimeout (incrementNumber, 1000 );