Analysis of javascript timer and javascript
SetTimeout () -- used to specify that a program is executed after a specific period of time.
Format:
[Timer object name =] setTimeout ("<expression>", in milliseconds );
Function: Run <expression> once.
The expression is a string that allows any javascript statement.
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
// Execute alert in 5 seconds
Function count (){
SetTimeout ("alert ('execution successfully');", 5000 );
}
</Script>
</Head>
<Body>
<Input type = "button" value = "run" onclick = "count ()">
</Body>
</Html>
SetInterval ()-repeated execution <expression> until the window and framework are closed or clearInterval is executed
Format: [Timer object name =] setInterval ("<expression>", millisecond)
ClearInterval () Termination Timer
Format: clearInterval (timer object name)
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var sec = 0;
Var timeId = setInterval ("count ();", 1000 );
Function count (){
Document. getElementById ("num"). innerHTML = sec ++;
}
Function stopCount (){
ClearInterval (timeId );
}
</Script>
</Head>
<Body>
<Font style = "color: red" id = "num"> 0 </font> seconds <input type = "button" value = "stop" onclick = "stopCount (); ">
</Body>
</Html>
Is it easy to use? If you need it, refer to it.