js中setInterval與setTimeout用法
JS里設(shè)定延時(shí):
使用SetInterval
和設(shè)定延時(shí)函數(shù)setTimeout
很類似予借。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ì)象
使用定時(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í),則可以在其中寫入要傳遞的參數(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”尽爆。其中最后一句也可以寫為: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
方法類似进每,可以調(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>
<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 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
表示的是變量。但這種寫法不夠直觀壤躲,而且有些場(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
很類似讶隐。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)用不同的清除方法:
例如:
var 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>
轉(zhuǎn)自:http://www.cnblogs.com/mercy/articles/2424882.html