JavaScript 計(jì)時(shí)事件
通過(guò)使用 JavaScript,我們有能力作到在一個(gè)設(shè)定的時(shí)間間隔之后來(lái)執(zhí)行代碼,而不是在函數(shù)被調(diào)用后立即執(zhí)行。我們稱(chēng)之為計(jì)時(shí)事件。
在 JavaScritp 中使用計(jì)時(shí)事件是很容易的,兩個(gè)關(guān)鍵方法是:
setInterval() - 間隔指定的毫秒數(shù)不停地執(zhí)行指定的代碼。
setTimeout() - 暫停指定的毫秒數(shù)后執(zhí)行指定的代碼
Note: setInterval() 和 setTimeout() 是 HTML DOM Window對(duì)象的兩個(gè)方法。
setInterval() 方法
setInterval() 間隔指定的毫秒數(shù)不停地執(zhí)行指定的代碼
語(yǔ)法
window.setInterval() 方法可以不使用window前綴,直接使用函數(shù)setInterval()。
setInterval() 第一個(gè)參數(shù)是函數(shù)(function)。
第二個(gè)參數(shù)間隔的毫秒數(shù)
注意: 1000 毫秒是一秒。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>單擊按鈕每3秒出現(xiàn)一個(gè)“Hello”警告框。</p> <p>再次點(diǎn)擊警告框,經(jīng)過(guò)3秒出現(xiàn)新的警告框,這將一直執(zhí)行 ...</p> <button onclick="myFunction()">點(diǎn)我</button> <script> function myFunction(){ setInterval(function(){alert("Hello")},3000); } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線(xiàn)實(shí)例
實(shí)例展示了如何使用 setInterval() 方法,但是每三秒彈出一次對(duì)用戶(hù)體驗(yàn)并不好。
以下實(shí)例將顯示當(dāng)前時(shí)間。 setInterval() 方法設(shè)置每秒鐘執(zhí)行一次代碼,就是手表一樣。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>在頁(yè)面顯示一個(gè)時(shí)鐘</p> <p id="demo"></p> <script> var myVar=setInterval(function(){myTimer()},1000); function myTimer(){ var d=new Date(); var t=d.toLocaleTimeString(); document.getElementById("demo").innerHTML=t; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線(xiàn)實(shí)例
如何停止執(zhí)行?
clearInterval() 方法用于停止 setInterval() 方法執(zhí)行的函數(shù)代碼。
語(yǔ)法
window.clearInterval() 方法可以不使用window前綴,直接使用函數(shù)clearInterval()。
要使用 clearInterval() 方法, 在創(chuàng)建計(jì)時(shí)方法時(shí)你必須使用全局變量:
然后你可以使用clearInterval() 方法來(lái)停止執(zhí)行。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>頁(yè)面上顯示時(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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線(xiàn)實(shí)例
setTimeout() 方法
語(yǔ)法
setTimeout() 方法會(huì)返回某個(gè)值。在上面的語(yǔ)句中,值被儲(chǔ)存在名為 t 的變量中。假如你希望取消這個(gè) setTimeout(),你可以使用這個(gè)變量名來(lái)指定它。
setTimeout() 的第一個(gè)參數(shù)是含有 JavaScript 語(yǔ)句的字符串。這個(gè)語(yǔ)句可能諸如 "alert('5 seconds!')",或者對(duì)函數(shù)的調(diào)用,諸如 alertMsg()"。
第二個(gè)參數(shù)指示從當(dāng)前起多少毫秒后執(zhí)行第一個(gè)參數(shù)。
提示:1000 毫秒等于一秒。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕,在等待 3 秒后彈出 "Hello"。</p> <button onclick="myFunction()">點(diǎn)我</button> <script> function myFunction(){ setTimeout(function(){alert("Hello")},3000); } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線(xiàn)實(shí)例
如何停止執(zhí)行?
clearTimeout() 方法用于停止執(zhí)行setTimeout()方法的函數(shù)代碼。
語(yǔ)法
window.clearTimeout() 方法可以不使用window 前綴。
要使用clearTimeout() 方法, 你必須在創(chuàng)建超時(shí)方法中(setTimeout)使用全局變量:
如果函數(shù)還未被執(zhí)行,你可以使用 clearTimeout() 方法來(lái)停止執(zhí)行函數(shù)代碼。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊第一個(gè)按鈕等待3秒后出現(xiàn)"Hello"彈框。</p> <p>點(diǎn)擊第二個(gè)按鈕來(lái)阻止第一個(gè)函數(shù)運(yùn)行。(你必須在3秒之前點(diǎn)擊它)。</p> <button onclick="myFunction()">點(diǎn)我</button> <button onclick="myStopFunction()">停止彈框</button> <script> var myVar; function myFunction(){ myVar=setTimeout(function(){alert("Hello")},3000); } function myStopFunction(){ clearTimeout(myVar); } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線(xiàn)實(shí)例
更多實(shí)例
另一個(gè)簡(jiǎn)單的計(jì)時(shí)
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <head> <script> function timedText(){ var x=document.getElementById('txt'); var t1=setTimeout(function(){x.value="2 seconds"},2000); var t2=setTimeout(function(){x.value="4 seconds"},4000); var t3=setTimeout(function(){x.value="6 seconds"},6000); } </script> </head> <body> <form> <input type="button" value="顯示文本時(shí)間!" onclick="timedText()" /> <input type="text" id="txt" /> </form> <p>點(diǎn)擊上面的按鈕,輸出的文本將告訴你2秒,4秒,6秒已經(jīng)過(guò)去了。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線(xiàn)實(shí)例