JavaScript timing events
JavaScript Timing Events
By using JavaScript, we have the ability to execute code after a set time interval, rather than immediately after the function is called. We call this a timing event.
After clicking the button in this example, a warning box will pop up after 5 seconds:
<html> <head> <script type="text/javascript"> function timedMsg() { var t=setTimeout("alert('5 秒!')",5000) } </script> </head> <body> <form> <input type="button" value="显示定时的警告框" onClick = "timedMsg()"> </form> <p>请点击上面的按钮。警告框会在 5 秒后显示。</p> </body> </html>
The program in this example will execute timings of 2 seconds, 4 seconds and 6 seconds:
<html> <head> <script type="text/javascript"> function timedText() { var t1=setTimeout("document.getElementById('txt').value='2 秒'",2000) var t2=setTimeout("document.getElementById('txt').value='4 秒'",4000) var t3=setTimeout("document.getElementById('txt').value='6 秒'",6000) } </script> </head> <body> <form> <input type="button" value="显示计时的文本" onClick="timedText()"> <input type="text" id="txt"> </form> <p>点击上面的按钮。输入框会显示出已经逝去的时间(2、4、6 秒)。</p> </body> </html>
In this example, after clicking the Start Timing button, the program starts counting in seconds from 0
<html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } </script> </head> <body> <form> <input type="button" value="开始计时!" onClick="timedCount()"> <input type="text" id="txt"> </form> <p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p> </body> </html>
In this example, after clicking the Count button, the program starts counting down based on the value entered by the user. Click the stop button to stop timing
<html> <head> <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } function stopCount() { c=0; setTimeout("document.getElementById('txt').value=0",0); clearTimeout(t); } </script> </head> <body> <form> <input type="button" value="开始计时!" onClick="timedCount()"> <input type="text" id="txt"> <input type="button" value="停止计时!" onClick="stopCount()"> </form> <p>请点击上面的“开始计时”按钮来启动计时器。输入框会一直进行计时,从 0 开始。点击“停止计时”按钮可以终止计时,并将计数重置为 0。</p> </body> </html>
A small JavaScript clock:
<html> <head> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById('txt').innerHTML=h+":"+m+":"+s t=setTimeout('startTime()',500) } function checkTime(i) { if (i<10) {i="0" + i} return i } </script> </head> <body onload="startTime()"> <div id="txt"></div> </body> </html>
JavaScript timing event
By using JavaScript, we have the ability to do it after a set time interval to execute the code instead of immediately after the function is called. We call this a timing event.
It is very easy to use timing events in JavaScript. The two key methods are:
setTimeout() execute the code at some time in the future clearTimeout() cancel setTimeout()
setTimeout()
Syntax
var t=setTimeout("javascript statement", milliseconds)
The setTimeout() method will return a certain value. In the above statement, the value is stored in a variable named t. If you wish to cancel this setTimeout(), you can specify it using this variable name.
The first parameter of setTimeout() is a string containing JavaScript statements. This statement may be such as "alert('5 seconds!')", or a call to a function such as alertMsg()".
The second parameter indicates how many milliseconds from the current time the first parameter will be executed.
Tip: 1000 milliseconds is equal to one second.
##How to stop execution?##clearInterval() method is used to stop setInterval. () function code executed by the method
Syntaxwindow.clearInterval(intervalVariable)
window.clearInterval() method can not use the window prefix. , use the function clearInterval() directly.
To use the clearInterval() method, you must use a global variable when creating the timing method:
myVar=setInterval("javascript function",milliseconds);
Then you can use clearInterval () method to stop execution.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>时钟停止示例</title> </head> <body> <p>页面上显示时钟:</p> <p id="demo"></p> <button onclick="myStopFunction()">停止时钟</button> <script> var myVar=setInterval(function(){myTimer()},1000); function myTimer(){ var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } function myStopFunction(){ clearInterval(myVar); } </script> </body> </html>