JavaScript 計時事件
JavaScript?計時事件
透過使用 JavaScript,我們有能力做到在一個設(shè)定的時間間隔之後來執(zhí)行程式碼,而不是在函數(shù)被呼叫後立即執(zhí)行。我們稱之為計時事件。
點(diǎn)擊本例中的按鈕後,會在5 秒後彈出一個警告框:
<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>請點(diǎn)擊上面的按鈕。警告框會在 5 秒后顯示。</p> </body> </html>
本例中的程式會執(zhí)行2 秒、4 秒和6 秒的計時:
<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>點(diǎn)擊上面的按鈕。輸入框會顯示出已經(jīng)逝去的時間(2、4、6 秒)。</p> </body> </html>
在本例中,點(diǎn)擊開始計時按鈕後,程式開始從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>請點(diǎn)擊上面的按鈕。輸入框會從 0 開始一直進(jìn)行計時。</p> </body> </html>
在本例中,點(diǎn)擊計數(shù)按鈕後根據(jù)使用者輸入的數(shù)值開始倒數(shù)計時,點(diǎn)選停止按鈕停止計時
<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>請點(diǎn)擊上面的“開始計時”按鈕來啟動計時器。輸入框會一直進(jìn)行計時,從 0 開始。點(diǎn)擊“停止計時”按鈕可以終止計時,并將計數(shù)重置為 0。</p> </body> </html>
一個JavaScript 小時鐘:
<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 計時事件
透過使用JavaScript,我們有能力作到在一個設(shè)定的時間間隔之後來執(zhí)行程式碼,而不是在函數(shù)被呼叫後立即執(zhí)行。我們稱之為計時事件。
在JavaScritp 中使用計時事件是很容易的,兩個關(guān)鍵方法是:
setTimeout()未來的某時執(zhí)行程式碼clearTimeout()取消setTimeout()
setTimeout()
語法
var t=setTimeout("javascript語句",毫秒)
setTimeout() 方法會傳回某個值。在上面的語句中,值被儲存在名為 t 的變數(shù)中。假如你希望取消這個 setTimeout(),你可以使用這個變數(shù)名稱來指定它。
setTimeout() 的第一個參數(shù)是含有 JavaScript 語句的字串。這個語句可能諸如"alert('5 seconds!')",或者對函數(shù)的調(diào)用,諸如alertMsg()"。
第二個參數(shù)指示從當(dāng)前起多少毫秒後執(zhí)行第一個參數(shù)。
提示:1000 毫秒等於一秒。 () 方法執(zhí)行的函數(shù)程式碼。 ,直接使用函數(shù)clearInterval()。
要使用clearInterval() 方法, 在建立計時方法時你必須使用全域變數(shù):
myVar=setInterval("javascript function",milliseconds);
然後你可以使用clearInterval () 方法來停止執(zhí)行。
<!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>