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="顯示定時(shí)的警告框" onClick = "timedMsg()"> </form> <p>請(qǐng)點(diǎn)擊上面的按鈕。警告框會(huì)在 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="顯示計(jì)時(shí)的文本" onClick="timedText()"> <input type="text" id="txt"> </form> <p>點(diǎn)擊上面的按鈕。輸入框會(huì)顯示出已經(jīng)逝去的時(shí)間(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="開始計(jì)時(shí)!" onClick="timedCount()"> <input type="text" id="txt"> </form> <p>請(qǐng)點(diǎn)擊上面的按鈕。輸入框會(huì)從 0 開始一直進(jìn)行計(jì)時(shí)。</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="開始計(jì)時(shí)!" onClick="timedCount()"> <input type="text" id="txt"> <input type="button" value="停止計(jì)時(shí)!" onClick="stopCount()"> </form> <p>請(qǐng)點(diǎn)擊上面的“開始計(jì)時(shí)”按鈕來啟動(dòng)計(jì)時(shí)器。輸入框會(huì)一直進(jìn)行計(jì)時(shí),從 0 開始。點(diǎn)擊“停止計(jì)時(shí)”按鈕可以終止計(jì)時(shí),并將計(jì)數(shù)重置為 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>時(shí)鐘停止示例</title> </head> <body> <p>頁面上顯示時(shí)鐘:</p> <p id="demo"></p> <button onclick="myStopFunction()">停止時(shí)鐘</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>