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