Timer 1
Used to specify that a program is executed after a specific period of time.
setTimeout():
Format: [Timer object name =] setTimeout("<expression>", milliseconds)
Function: Execute <expression> once.
example:
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>timer1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function count()
{
setTimeout("alert('Execution successful!')",7000);
}
</script>
</head>
<body>
<input type="button" value="click me" onclick="count();">
</body>
</html>
Timer 2
Execute expressions repeatedly at a certain time interval.
setInterval():
Format: [Timer object name =] setInterval("<expression>", milliseconds)
Function: Repeat the <expression> until the window and frame are closed or clearInterval is executed.
clearInterval():
Format: clearInterval (timer object name)
Function: Terminate timer
example:
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>timer2.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
var sec = 0;
var timer = setInterval("count();",1000);//Time starts when the page is loading
function count()
{
document.getElementById("num").innerHTML = sec++;
}
function stopCount()
{
clearInterval(timer);//Stop the running of the timer
}
</script>
</head>
<body>
<font color="red" id="num">0</font>
<input type="button" value="stop" onclick="stopCount();">
</body>
</html>
The above is the entire content of this article. I hope you like it