javascript計(jì)時(shí)器事件使用詳解
在 JavaScritp 中使用計(jì)時(shí)事件是很容易的,兩個(gè)關(guān)鍵方法是:
setTimeout()
未來(lái)的某時(shí)執(zhí)行代碼
clearTimeout()
取消setTimeout()
setTimeout()
語(yǔ)法
var t=setTimeout("javascript語(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 毫秒等于一秒。
當(dāng)下面這個(gè)例子中的按鈕被點(diǎn)擊時(shí),一個(gè)提示框會(huì)在5秒中后彈出。
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!" onClick="timedMsg()">
</form>
</body>
</html>
實(shí)例 - 無(wú)窮循環(huán)
要?jiǎng)?chuàng)建一個(gè)運(yùn)行于無(wú)窮循環(huán)中的計(jì)時(shí)器,我們需要編寫(xiě)一個(gè)函數(shù)來(lái)調(diào)用其自身。在下面的例子中,當(dāng)按鈕被點(diǎn)擊后,輸入域便從 0 開(kāi)始計(jì)數(shù)。
<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="Start count!" onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>
</html>
clearTimeout()
語(yǔ)法
clearTimeout(setTimeout_variable)
實(shí)例
下面的例子和上面的無(wú)窮循環(huán)的例子相似。唯一的不同是,現(xiàn)在我們添加了一個(gè) "Stop Count!" 按鈕來(lái)停止這個(gè)計(jì)數(shù)器:
<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()
{
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>
另外兩個(gè)重要的方法:
setInterval()
setInterval() - executes a function, over and over again, at specified time intervals
作用是:循環(huán)執(zhí)行一個(gè)方法,在規(guī)定的間隔時(shí)間內(nèi)
語(yǔ)法:
window.setInterval("javascript function",milliseconds);
說(shuō)明:第一個(gè)參數(shù)必須是一個(gè)函數(shù),第二個(gè)參數(shù)是執(zhí)行函數(shù)的間隔時(shí)間.
實(shí)例:
<html>
<script type="text/javascript">
setInterval(function() {alert("hello")},500);
</script>
</html>
說(shuō)明:上面例子,執(zhí)行效果是說(shuō)每隔500ms就alert("hello");
再來(lái)一個(gè)時(shí)鐘:
<html>
<body>
<p id="demo" ></p>
<script type="text/javascript">
setInterval(function(){ myTimer()},1000);
function myTimer(){
var d = new Date();
var t=d.toLocaleTimeString();
document.getElementById('demo').innerHTML=t;
}
</script>
</body>
</html>
如何停止,setInterval()方法??
window.clearInterval()
語(yǔ)法:
window.clearInterval(intervalVariable)
The window.clearInterval() method can be written without the window prefix.
To be able to use the clearInterval() method, you must use a global variable when creating the interval method:
myVar=setInterval("javascript function",milliseconds);
Then you will be able to stop the execution by calling the clearInterval() method.
實(shí)例:
<html>
<body>
<p id="demo" ></p>
<p id="demo2" onclick="stop()">stop</p>
<script type="text/javascript">
var temp=setInterval(function(){ myTimer()},1000);
function myTimer(){
var d = new Date();
var t=d.toLocaleTimeString();
document.getElementById('demo').innerHTML=t;
}
function stop(){
<html>
<body>
<p id="demo" ></p>
<p id="demo2" onclick="stop()">stop</p>
<script type="text/javascript">
var temp=setInterval(function(){ myTimer()},1000);
function myTimer(){
var d = new Date();
var t=d.toLocaleTimeString();
document.getElementById('demo').innerHTML=t;
}
function stop(){
clearInterval(temp);
}
</script>
</body>
</html>
}
</script>
</body>
</html>
相關(guān)文章
詳解js動(dòng)態(tài)獲取瀏覽器或頁(yè)面等容器的寬高
這篇文章主要介紹了js動(dòng)態(tài)獲取瀏覽器或頁(yè)面等容器的寬高,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03簡(jiǎn)介JavaScript中Math.LOG10E屬性的使用
這篇文章主要介紹了JavaScript中Math.LOG10E屬性的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06javascript學(xué)習(xí)筆記(八) js內(nèi)置對(duì)象
javascript學(xué)習(xí)筆記之js內(nèi)置對(duì)象,需要的朋友可以參考下2012-06-06JavaScript數(shù)據(jù)類型相關(guān)知識(shí)詳解
這篇文章主要介紹了JavaScript數(shù)據(jù)類型相關(guān)知識(shí)詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)js數(shù)據(jù)類型的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04