JavaScript 計(jì)時(shí)事件
JavaScript 計(jì)時(shí)事件
通過(guò)使用 JavaScript,我們有能力作到在一個(gè)設(shè)定的時(shí)間間隔之后來(lái)執(zhí)行代碼,而不是在函數(shù)被調(diào)用后立即執(zhí)行。我們稱之為計(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("javascript function",milliseconds);
window.setInterval()?方法可以不使用window前綴,直接使用函數(shù)setInterval()。
setInterval() 第一個(gè)參數(shù)是函數(shù)(function)。
第二個(gè)參數(shù)間隔的毫秒數(shù)
注意:?1000 毫秒是一秒。
每三秒彈出 "hello" :
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <button onclick="myFunction()">點(diǎn)我</button> <script> function myFunction(){ setInterval(function(){alert("Hello")},3000); } </script> </body> </html>
如何停止執(zhí)行?
clearInterval() 方法用于停止 setInterval() 方法執(zhí)行的函數(shù)代碼。
語(yǔ)法
window.clearInterval(intervalVariable)
window.clearInterval()?方法可以不使用window前綴,直接使用函數(shù)clearInterval()。
要使用 clearInterval() 方法, 在創(chuàng)建計(jì)時(shí)方法時(shí)你必須使用全局變量:
myVar=setInterval("javascript function",milliseconds);
然后你可以使用clearInterval() 方法來(lái)停止執(zhí)行。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> var t = self.setInterval("clock()",50) function clock() { var time=new Date() document.getElementById("clock_show").innerHTML=time } </script> </head> <body> <p id="clock_show"></p> <button onclick="window.clearInterval(t)">停止計(jì)時(shí)</button> </body> </html>
setTimeout() 方法
語(yǔ)法
window.setTimeout("javascript 函數(shù)",毫秒數(shù));
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 毫秒等于一秒。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <button onclick="myFunction()">點(diǎn)我</button> <script> function myFunction(){ setTimeout(function(){alert("Hello")},3000); } </script> </body> </html>
如何停止執(zhí)行?
clearTimeout() 方法用于停止執(zhí)行setTimeout()方法的函數(shù)代碼。
語(yǔ)法
window.clearTimeout(timeoutVariable)
window.clearTimeout()?方法可以不使用window 前綴。
要使用clearTimeout() 方法, 你必須在創(chuàng)建超時(shí)方法中(setTimeout)使用全局變量:
myVar=setTimeout("javascript function",milliseconds);
如果函數(shù)還未被執(zhí)行,你可以使用 clearTimeout() 方法來(lái)停止執(zhí)行函數(shù)代碼。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> var s = 0; var n = 0; var t; function timeCount() { document.getElementById('second_show').value = s; n = n+1; s = n/10; t = setTimeout("timeCount()",100); } </script> </head> <body> <form> <input type="button" value="開(kāi)始計(jì)時(shí)" onClick="timeCount()"> <input type="text" id="second_show"> <input type="button" value="結(jié)束計(jì)時(shí)" onClick="clearTimeout(t)"> </form> </body> </html>