欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript中SetInterval與setTimeout的用法詳解

 更新時(shí)間:2015年11月10日 09:28:25   投稿:mrr  
在寫(xiě)H5游戲時(shí)經(jīng)常需要使用定時(shí)刷新頁(yè)面實(shí)現(xiàn)動(dòng)畫(huà)效果,比較常用即setTimeout()以及setInterval(),但是大家對(duì)SetInterval與setTimeout的用法了解嗎,下面通過(guò)本文給大家詳解js中SetInterval與setTimeout的用法,需要的朋友參考下

setTimeout

描述

setTimeout(code,millisec)

setTimeout() 方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計(jì)算表達(dá)式。

注:調(diào)用過(guò)程中,可以使用clearTimeout(id_of_settimeout)終止

參數(shù) 描述
code 必需,要調(diào)用的函數(shù)后要執(zhí)行的 JavaScript 代碼串。
millisec 必需,在執(zhí)行代碼前需等待的毫秒數(shù)。

setTimeinterval

setInterval(code,millisec[,"lang"])

參數(shù) 描述
code 必需,要調(diào)用的函數(shù)或要執(zhí)行的代碼串。
millisec 必需,周期性執(zhí)行或調(diào)用code之間的時(shí)間間隔,以毫秒計(jì)。

setInterval() 方法可按照指定的周期(以毫秒計(jì))來(lái)調(diào)用函數(shù)或計(jì)算表達(dá)式。

JS里設(shè)定延時(shí):

使用SetInterval和設(shè)定延時(shí)函數(shù)setTimeout 很類(lèi)似。setTimeout 運(yùn)用在延遲一段時(shí)間,再進(jìn)行某項(xiàng)操作。

setTimeout("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象 setInterval("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象

SetInterval為自動(dòng)重復(fù),setTimeout不會(huì)重復(fù)。

clearTimeout(對(duì)象) 清除已設(shè)置的setTimeout對(duì)象 clearInterval(對(duì)象) 清除已設(shè)置的setInterval對(duì)象

setInterval() 方法可按照指定的周期(以毫秒計(jì))來(lái)調(diào)用函數(shù)或計(jì)算表達(dá)式。

使用定時(shí)器實(shí)現(xiàn)JavaScript的延期執(zhí)行或重復(fù)執(zhí)行 window對(duì)象提供了兩個(gè)方法來(lái)實(shí)現(xiàn)定時(shí)器的效果,分別是window.setTimeout()和window.setInterval。其中前者可以使一段代碼在指定時(shí)間后運(yùn)行;而后者則可以使一段代碼每過(guò)指定時(shí)間就運(yùn)行一次。它們的原型如下: window.setTimeout(expression,milliseconds); window.setInterval(expression,milliseconds); 其中,expression可以是用引號(hào)括起來(lái)的一段代碼,也可以是一個(gè)函數(shù)名,到了指定的時(shí)間,系統(tǒng)便會(huì)自動(dòng)調(diào)用該函數(shù),當(dāng)使用函數(shù)名作為調(diào)用句柄時(shí),不能帶有任何參數(shù);而使用字符串時(shí),則可以在其中寫(xiě)入要傳遞的參數(shù)。兩個(gè)方法的第二個(gè)參數(shù)是milliseconds,表示延時(shí)或者重復(fù)執(zhí)行的毫秒數(shù)。

下面分別介紹兩種方法。

1.window.setTimeout方法 該方法可以延時(shí)執(zhí)行一個(gè)函數(shù),例如:

<script language="JavaScript" type="text/javascript">
<!--
 function hello(){ alert("hello"); } window.setTimeout(hello,5000);
//-->
 </script>

這段代碼將使得頁(yè)面打開(kāi)5秒鐘后顯示對(duì)話框“hello”。其中最后一句也可以寫(xiě)為: window.setTimeout("hello()",5000); 讀者可以體會(huì)它們的差別,在window.setInterval方法中也有這樣的性質(zhì)。 如果在延時(shí)期限到達(dá)之前取消延執(zhí)行,可以使用window.clearTimeout(timeoutId)方法,該方法接收一個(gè)id,表示一個(gè)定時(shí)器。這個(gè)id是由setTimeout方法返回的,例如:

<script language="JavaScript" type="text/javascript">
<!--
function hello(){   
alert("hello");
}
var id=window.setTimeout(hello,5000);
document.onclick=function(){   
window.clearTimeout(id);
 }
//-->
</script>

這樣,如果要取消顯示,只需單擊頁(yè)面任何一部分,就執(zhí)行了window.clearTimeout方法,使得超時(shí)操作被取消。

2.window.setInterval方法 該方法使得一個(gè)函數(shù)每隔固定時(shí)間被調(diào)用一次,是一個(gè)很常用的方法。如果想要取消定時(shí)執(zhí)行,和clearTimeout方法類(lèi)似,可以調(diào)用window.clearInterval方法。clearInterval方法同樣接收一個(gè)setInterval方法返回的值作為參數(shù)。例如: //定義一個(gè)反復(fù)執(zhí)行的調(diào)用 var id=window.setInterval("somefunction",10000); //取消定時(shí)執(zhí)行 window.clearInterval(id); 上面的代碼僅用于說(shuō)明怎樣取消一個(gè)定時(shí)執(zhí)行。實(shí)際上在很多場(chǎng)合都需要用到setInterval方法,下面將設(shè)計(jì)一個(gè)秒表,來(lái)介紹setInterval函數(shù)的用途:該秒表將包括兩個(gè)按鈕和一個(gè)用于顯示時(shí)間的文本框。當(dāng)單擊開(kāi)始按鈕時(shí)開(kāi)始計(jì)時(shí),最小單位為0.01秒,此時(shí)再次單擊按鈕則停止計(jì)時(shí),文本框顯示經(jīng)過(guò)的時(shí)間。另外一個(gè)按鈕用于將當(dāng)前時(shí)間清零。其實(shí)現(xiàn)代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head>
 <title> New Document </title>
 </head> 
<body> 
<form action="somepage.asp"> 
<input type="text" value="0" name="txt1"/> 
<input type="button" value="開(kāi)始" name="btnStart"/> 
<input type="button" value="重置" name="btnReset"/> 
</form> 
</body> 
</html>

<script language="JavaScript" type="text/javascript">
<!--
//獲取表單中的表單域
var txt=document.forms[0].elements["txt1"];
 var btnStart=document.forms[0].elements["btnStart"];
 var btnReset=document.forms[0].elements["btnReset"]
 //定義定時(shí)器的id
var id;
//每10毫秒該值增加1
var seed=0;
btnStart.onclick=function(){   
//根據(jù)按鈕文本來(lái)判斷當(dāng)前操作   
 if(this.value=="開(kāi)始"){       
 //使按鈕文本變?yōu)橥V?      
 this.value="停止";       
//使重置按鈕不可用       
 btnReset.disabled=true;       
//設(shè)置定時(shí)器,每0.01s跳一次       
id=window.setInterval(tip,10);    }
else{       
//使按鈕文本變?yōu)殚_(kāi)始       
this.value="開(kāi)始";       
//使重置按鈕可用       
 btnReset.disabled=false;       
//取消定時(shí)       
window.clearInterval(id);   
 } }
//重置按鈕
btnReset.onclick=function(){   
seed=0;
 }
//讓秒表跳一格
 function tip(){  
  seed++;   
 txt.value=seed/100;
}
//-->
</script>

給定時(shí)器調(diào)用傳遞參數(shù) 無(wú)論是window.setTimeout還是window.setInterval,在使用函數(shù)名作為調(diào)用句柄時(shí)都不能帶參數(shù),而在許多場(chǎng)合必須要帶參數(shù),這就需要想方法解決。例如對(duì)于函數(shù)hello(_name),它用于針對(duì)用戶名顯示歡迎信息: var userName="jack";

//根據(jù)用戶名顯示歡迎信息
function hello(_name){   
 alert("hello,"+_name);
 }

這時(shí),如果企圖使用以下語(yǔ)句來(lái)使hello函數(shù)延遲3秒執(zhí)行是不可行的:

 window.setTimeout(hello(userName),3000);

這將使hello函數(shù)立即執(zhí)行,并將返回值作為調(diào)用句柄傳遞給setTimeout函數(shù),其結(jié)果并不是程序需要的。而使用字符串形式可以達(dá)到想要的結(jié)果:

window.setTimeout("hello(userName)",3000);

這里的字符串是一段JavaScript代碼,其中的userName表示的是變量。但這種寫(xiě)法不夠直觀,而且有些場(chǎng)合必須使用函數(shù)名,下面用一個(gè)小技巧來(lái)實(shí)現(xiàn)帶參數(shù)函數(shù)的調(diào)用:

<script language="JavaScript" type="text/javascript"> <!-- var userName="jack";
//根據(jù)用戶名顯示歡迎信息
function hello(_name){    
alert("hello,"+_name);
}
//創(chuàng)建一個(gè)函數(shù),用于返回一個(gè)無(wú)參數(shù)函數(shù)
function _hello(_name){    
return function(){       
hello(_name);    } }
window.setTimeout(_hello(userName),3000);
 //-->
</script>

這里定義了一個(gè)函數(shù)_hello,用于接收一個(gè)參數(shù),并返回一個(gè)不帶參數(shù)的函數(shù),在這個(gè)函數(shù)內(nèi)部使用了外部函數(shù)的參數(shù),從而對(duì)其調(diào)用,不需要使用參數(shù)。在window.setTimeout函數(shù)中,使用_hello(userName)來(lái)返回一個(gè)不帶參數(shù)的函數(shù)句柄,從而實(shí)現(xiàn)了參數(shù)傳遞的功能。

window對(duì)象有兩個(gè)主要的定時(shí)方法,分別是setTimeout 和 setInteval 他們的語(yǔ)法基本上相同,但是完成的功能取有區(qū)別。

  setTimeout方法是定時(shí)程序,也就是在什么時(shí)間以后干什么。干完了就拉倒。

  setInterval方法則是表示間隔一定時(shí)間反復(fù)執(zhí)行某操作。

  JS里設(shè)定延時(shí):

使用SetInterval和設(shè)定延時(shí)函數(shù)setTimeout 很類(lèi)似。setTimeout 運(yùn)用在延遲一段時(shí)間,再進(jìn)行某項(xiàng)操作。

setTimeout("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象

setInterval("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象

SetInterval為自動(dòng)重復(fù),setTimeout不會(huì)重復(fù)。

clearTimeout(對(duì)象) 清除已設(shè)置的setTimeout對(duì)象

clearInterval(對(duì)象) 清除已設(shè)置的setInterval對(duì)象

如果用setTimeout實(shí)現(xiàn)setInerval的功能,就需要在執(zhí)行的程序中再定時(shí)調(diào)用自己才行。如果要清除計(jì)數(shù)器需要根據(jù)使用的方法不同,調(diào)用不同的清除方法:

例如:

tttt=setTimeout('northsnow()',1000);
clearTimeout(tttt);

或者:

tttt=setInterval('northsnow()',1000);
clearInteval(tttt);

舉一個(gè)例子:

<div id="liujincai">
</div>
<input type="button" name="start" value="start" onclick='startShow();'>
<input type="button" name="stop" value="stop" onclick="stop();">
<script language="javascript">  
var intvalue=1;  
var timer2=null;  
function startShow()  {   
 liujincai.innerHTML=liujincai.innerHTML + " " + (intvalue ++).toString();   
timer2=window.setTimeout("startShow()",2000);  }  
function stop()  {   
 window.clearTimeout(timer2);  
 }
</script>

或者:

<div id="liujincai">
</div>
<input type="button" name="start" value="start" onclick='timer2=window.setInterval("startShow()",2000);//startShow();'>
<input type="button" name="stop" value="stop" onclick="stop();">
<script language="javascript">  
 var intvalue=1;  
var timer2=null;  
 function startShow()  {   
 liujincai.innerHTML=liujincai.innerHTML + " " + (intvalue ++).toString();  
 }  
 function stop()  {   
 window.clearInterval(timer2);  
}
</script>

以上內(nèi)容是小編給大家介紹的關(guān)于JavaScript中SetInterval與setTimeout的用法詳解,希望對(duì)大家學(xué)習(xí)SetInterval與setTimeout的相關(guān)知識(shí)有所幫助。

相關(guān)文章

最新評(píng)論