在javascritp中,有兩個(gè)關(guān)于定時(shí)器的專用函數(shù),分別為:
1.倒計(jì)定時(shí)器:timename=setTimeout("function();",delaytime);
2.循環(huán)定時(shí)器:timename=setInterval("function();",delaytime);
第一個(gè)參數(shù)“function()”是定時(shí)器觸發(fā)時(shí)要執(zhí)行的動(dòng)作,可以是一個(gè)函數(shù),也可以是幾個(gè)函數(shù),函數(shù)間用“;”隔開即可。比如要彈出兩個(gè)警告窗口,便可將“function();”換成
“alert('第一個(gè)警告窗口!');alert('第二個(gè)警告窗口!');”;而第二個(gè)參數(shù)“delaytime”則是間隔的時(shí)間,以毫秒為單位,即填寫“5000”,就表示5秒鐘。
倒計(jì)時(shí)定時(shí)器是在指定時(shí)間到達(dá)后觸發(fā)事件,而循環(huán)定時(shí)器就是在間隔時(shí)間到來時(shí)反復(fù)觸發(fā)事件,兩者的區(qū)別在于:前者只是作用一次,而后者則不停地作用。
比如你打開一個(gè)頁(yè)面后,想間隔幾秒自動(dòng)跳轉(zhuǎn)到另一個(gè)頁(yè)面,則你就需要采用倒計(jì)定時(shí)器“setTimeout("function();",delaytime)”,而如果想將某一句話設(shè)置成一個(gè)一個(gè)字的出現(xiàn),
則需要用到循環(huán)定時(shí)器“setInterval("function();",delaytime)” 。
獲取表單的焦點(diǎn),則用到document.activeElement.id。利用if來判斷document.activeElement.id和表單的ID是否相同。
比如:if ("mid" == document.activeElement.id){alert();},"mid"便是表單對(duì)應(yīng)的ID。
定時(shí)器:
用以指定在一段特定的時(shí)間后執(zhí)行某段程序。
JS中定時(shí)執(zhí)行,setTimeout和setInterval的區(qū)別,以及l(fā)解除方法
setTimeout(Expression,DelayTime),在DelayTime過后,將執(zhí)行一次Expression,setTimeout運(yùn)用在延遲一段時(shí)間,再進(jìn)行某項(xiàng)操作。
setTimeout("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象
setInterval(expression,delayTime),每個(gè)DelayTime,都將執(zhí)行Expression.常??捎糜谒⑿卤磉_(dá)式.
setInterval("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象
SetInterval為自動(dòng)重復(fù),setTimeout不會(huì)重復(fù)。
clearTimeout(對(duì)象) 清除已設(shè)置的setTimeout對(duì)象
clearInterval(對(duì)象) 清除已設(shè)置的setInterval對(duì)象
略舉兩例。
例1.表單觸發(fā)或加載時(shí),逐字輸出字符串
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script language="JavaScript"type="text/javascript">
var str = "這個(gè)是測(cè)試用的范例文字";
var seq = 0;
var second=1000; //間隔時(shí)間1秒鐘
function scroll() {
msg = str.substring(0, seq+1);
document.getElementByIdx_x_x('word').innerHTML = msg;
seq++;
if (seq >= str.length) seq = 0;
}
</script>
</head>
<bodyonload="setInterval('scroll()',second)">
<divid="word"></div><br/><br/>
</body>
</html>
例2.當(dāng)焦點(diǎn)在輸入框的時(shí)候,定時(shí)檢查輸入框信息,焦點(diǎn)不在時(shí)不執(zhí)行檢查動(dòng)作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script language="JavaScript"type="text/javascript">
var second=5000; //間隔時(shí)間5秒鐘
var c=0;
function scroll() {
c++;
if ("b" == document.activeElement.id) {
var str="定時(shí)檢查第<b> "+c+"</b>次<br/>";
if(document.getElementByIdx_x_x('b').value!=""){
str+="輸入框當(dāng)前內(nèi)容為當(dāng)前內(nèi)容為<br/><b>"+document.getElementByIdx_x_x('b').value+"</b>";
}
document.getElementByIdx_x_x('word').innerHTML = str;
}
}
</script>
</head>
<body>
<textarea id="b" name="b"onfocus="setInterval('scroll()',second)"></textarea><br/><br/>
<divid="word"></div><br/><br/>
</body>
</html>
例3.下面這個(gè)是最簡(jiǎn)單的例子,定時(shí)器時(shí)間到達(dá)后彈出警告窗口。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<script language="javascript">
function count() {
document.getElementByIdx_x_x('m').innerHTML="計(jì)時(shí)已經(jīng)開始!";
setTimeout("alert('十秒鐘到!')",10000)
}
</script>
<body>
<divid="m"></div>
<input TYPE="button" value=" 計(jì)時(shí)開始"onclick="count()">
</body>
</html>
例4:倒計(jì)時(shí)定時(shí)跳轉(zhuǎn)
<head>
<basehref="<%=basePath%>">
<title>MyJSP 'ds04.jsp' startingpage</title>
<spanid="tiao">3</span>
<ahref="javascript:countDown"></a>秒后自動(dòng)跳轉(zhuǎn)……
<metahttp-equiv=refresh content=3;url= '/ds02.jsp'/>
<!--腳本開始-->
<scriptlanguage="javascript" type="">
function countDown(secs){
tiao.innerText=secs;
if(--secs>0)
setTimeout("countDown("+secs+")",1000);
}
countDown(3);
</script>
<!--腳本結(jié)束-->
</head>
例6:
<head><meta http-equiv="refresh"content="2;url='b.html'">
</head>例7:<script language="javascript"type="text/javascript">
setTimeout("window.location.href='b.html'",2000);
//下面兩個(gè)都可以用
//setTimeout("javascript:location.href='b.html'",2000);
//setTimeout("window.location='b.html'",2000);
</script>例8:<spanid="totalSecond">2</span><script language="javascript"type="text/javascript">var second =document.getElementByIdx_x('totalSecond').innerHTML;if(isNaN(second)){
//……不是數(shù)字的處理方法
}else{
setInterval(function(){
document.getElementByIdx_x('totalSecond').innerHTML= --second;
if (second<= 0) {
window.location= 'b.html';
}
}, 1000);
}
</script>
愛華網(wǎng)


