A brief analysis of javascript timer

  • 2020-05-09 18:05:00
  • OfStack

setTimeout()-- used to specify that a program should be executed after a specified period of time.

                  format:

[timer object name =]setTimeout(" < expression > ", milliseconds);

Function: execution < expression > 1 times.

Where the expression is a string that can make any javascript statement


<html>
<head>
<script type="text/javascript">
//5 Execute after seconds alert
function count(){
    setTimeout("alert(' Execute successfully ');",5000);
}
</script>
</head>
<body>
<input type="button" value=" perform " onclick="count()">
</body>
</html>

setInterval() - repeat execution < expression > , until the window, frame is closed or clearInterval is executed

Format: [timer object name =]setInterval(" < expression > ", ms)

clearInterval() terminates the timer

Format: clearInterval(timer object name)


<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 not very good to use it, there is a need for the small friend reference.


Related articles: