亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript計(jì)時(shí)器事件使用詳解

 更新時(shí)間:2014年01月07日 15:26:43   作者:  
通過(guò)使用JavaScript,能在一個(gè)設(shè)定的時(shí)間間隔之后來(lái)執(zhí)行代碼,而不是在函數(shù)被調(diào)用后立即執(zhí)行。我們稱之為計(jì)時(shí)事件,下面對(duì)javascript計(jì)時(shí)器事件做了詳細(xì)解釋

在 JavaScritp 中使用計(jì)時(shí)事件是很容易的,兩個(gè)關(guān)鍵方法是:

setTimeout()
未來(lái)的某時(shí)執(zhí)行代碼

clearTimeout()
取消setTimeout()
setTimeout()
語(yǔ)法

復(fù)制代碼 代碼如下:

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秒中后彈出。

復(fù)制代碼 代碼如下:

<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ù)。

復(fù)制代碼 代碼如下:

<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ǔ)法

復(fù)制代碼 代碼如下:

clearTimeout(setTimeout_variable)
 

實(shí)例

下面的例子和上面的無(wú)窮循環(huán)的例子相似。唯一的不同是,現(xiàn)在我們添加了一個(gè) "Stop Count!" 按鈕來(lái)停止這個(gè)計(jì)數(shù)器:

復(fù)制代碼 代碼如下:

<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è)重要的方法:
復(fù)制代碼 代碼如下:

setInterval()
setInterval() - executes a function, over and over again, at specified time intervals

作用是:循環(huán)執(zhí)行一個(gè)方法,在規(guī)定的間隔時(shí)間內(nèi)

語(yǔ)法:

復(fù)制代碼 代碼如下:

window.setInterval("javascript function",milliseconds);

說(shuō)明:第一個(gè)參數(shù)必須是一個(gè)函數(shù),第二個(gè)參數(shù)是執(zhí)行函數(shù)的間隔時(shí)間.

實(shí)例:

復(fù)制代碼 代碼如下:

<html>
<script type="text/javascript">
setInterval(function() {alert("hello")},500);
</script>
</html>

說(shuō)明:上面例子,執(zhí)行效果是說(shuō)每隔500ms就alert("hello");

再來(lái)一個(gè)時(shí)鐘:

復(fù)制代碼 代碼如下:

<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()方法??

復(fù)制代碼 代碼如下:

window.clearInterval()

語(yǔ)法:
復(fù)制代碼 代碼如下:

window.clearInterval(intervalVariable)


復(fù)制代碼 代碼如下:

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í)例:

復(fù)制代碼 代碼如下:

<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)文章

最新評(píng)論