JS Timing
使用JS是可以讓函數(shù)不直接執(zhí)行的,而是在過(guò)了一個(gè)指定的時(shí)間間隔后才執(zhí)行。這就叫做事件事件。
With JavaScript, it is possible to execute some code NOT immediately after a function is called, but after a specified time interval. This is called timing events.
使用JS是可以讓函數(shù)不直接執(zhí)行的,而是在過(guò)了一個(gè)指定的時(shí)間間隔后才執(zhí)行。這就叫做事件事件。
JavaScript Timing Events
JS時(shí)間事件
With JavaScript, it is possible to execute some code NOT immediately after a function is called, but after a specified time interval. This is called timing events.
使用JS是可以讓函數(shù)不直接執(zhí)行的,而是在過(guò)了一個(gè)指定的時(shí)間間隔后才執(zhí)行。這就叫做事件事件。
It's very easy to time events in JavaScript. The two key methods that are used are:
JS的時(shí)間事件是非常簡(jiǎn)單的。使用了兩個(gè)關(guān)鍵的方法:
* setTimeout() - executes a code some time in the future
在一些時(shí)間后執(zhí)行代碼
* clearTimeout() - cancels the setTimeout()
取消setTimeout()
Note: The setTimeout() and clearTimeout() are both methods of the HTML DOM Window object.
注意:setTimeout() 和 Timeout() 都是HTML DOM Window 對(duì)象的方法。
setTimeout()
Syntax語(yǔ)法
var t=setTimeout("javascript statement",milliseconds)
The setTimeout() method returns a value - In the statement above, the value is stored in a variable called t. If you want to cancel this setTimeout(), you can refer to it using the variable name.
setTimeout()方法返回一個(gè)值 - 在上面的聲明里,值被保存在變量t中。如果你想取消這個(gè)setTimeout()可以使用變量名來(lái)提出它(用clearTimeout(t))
The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('5 seconds!')" or a call to a function, like "alertMsg()".
setTomeout()的第一個(gè)參數(shù)是字符串聲明。它可以像"alert('5 seconds!')"或是調(diào)用一個(gè)函數(shù)像"alertMsg()"
The second parameter indicates how many milliseconds from now you want to execute the first parameter.
第二個(gè)參數(shù)用來(lái)表明從現(xiàn)在開(kāi)始你希望在多少毫秒后執(zhí)行第一個(gè)參數(shù)
Note: There are 1000 milliseconds in one second.
1000毫秒為一秒
Example
舉例
When the button is clicked in the example below, an alert box will be displayed after 5 seconds.
當(dāng)下面的按鈕被點(diǎn)擊后,每過(guò)5秒就會(huì)出現(xiàn)一個(gè)警告框。
<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>
Example - Infinite Loop
無(wú)限循環(huán)
To get a timer to work in an infinite loop, we must write a function that calls itself. In the example below, when the button is clicked, the input field will start to count (for ever), starting at 0:
要得到一個(gè)無(wú)限循環(huán)的記時(shí)器,我們必須寫(xiě)出一個(gè)自我調(diào)用的函數(shù)。下面的例子,當(dāng)按鈕按下后,輸入框就會(huì)從0開(kāi)始記數(shù)(永遠(yuǎ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)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!"
onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>
</html>
clearTimeout()
Syntax語(yǔ)法
clearTimeout(setTimeout_variable)
Example
舉例
The example below is the same as the "Infinite Loop" example above. The only difference is that we have now added a "Stop Count!" button that stops the timer:
下面的例子和上面的“無(wú)限循環(huán)”差不多。唯一的不同就是我們現(xiàn)在多了一個(gè)“停止記數(shù)”的按鈕來(lái)停止記時(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>
相關(guān)文章
利用javascript數(shù)組長(zhǎng)度循環(huán)數(shù)組內(nèi)所有元素
javascript循環(huán)數(shù)組內(nèi)所有元素代碼學(xué)習(xí),大家參考使用吧2013-12-12
可簡(jiǎn)單避免的三個(gè)JS發(fā)布錯(cuò)誤的詳細(xì)介紹
這篇文章詳細(xì)介紹了可簡(jiǎn)單避免的三個(gè)JS發(fā)布錯(cuò)誤,有需要的朋友可以參考一下2013-08-08
深入理解JavaScript系列(45):代碼復(fù)用模式(避免篇)詳解
這篇文章主要介紹了深入理解JavaScript系列(45):代碼復(fù)用模式(避免篇)詳解,本文講解了默認(rèn)模式、借用構(gòu)造函數(shù)、借用構(gòu)造函數(shù)并設(shè)置原型、共享原型、臨時(shí)構(gòu)造函數(shù)、klass等內(nèi)容,需要的朋友可以參考下2015-03-03
Javascript實(shí)例教程(19) 使用HoTMetal(2)
Javascript實(shí)例教程(19) 使用HoTMetal(2)...2006-12-12
JavaScript字符串對(duì)象substring方法入門(mén)實(shí)例(用于截取字符串)
這篇文章主要介紹了JavaScript字符串對(duì)象substring方法入門(mén)實(shí)例,substring方法通過(guò)指定開(kāi)始和結(jié)束位置來(lái)截取字符串,需要的朋友可以參考下2014-10-10
淺談JavaScript 標(biāo)準(zhǔn)對(duì)象
下面小編就為大家?guī)?lái)一篇淺談JavaScript 標(biāo)準(zhǔn)對(duì)象。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06

