js定時器的使用(實例講解)
在javascritp中,有兩個關(guān)于定時器的專用函數(shù),分別為:
1.倒計定時器:timename=setTimeout("function();",delaytime);
2.循環(huán)定時器:timename=setInterval("function();",delaytime);
第一個參數(shù)“function()”是定時器觸發(fā)時要執(zhí)行的動作,可以是一個函數(shù),也可以是幾個函數(shù),函數(shù)間用“;”隔開即可。比如要彈出兩個警告窗口,便可將“function();”換成
“alert('第一個警告窗口!');alert('第二個警告窗口!');”;而第二個參數(shù)“delaytime”則是間隔的時間,以毫秒為單位,即填寫“5000”,就表示5秒鐘。
倒計時定時器是在指定時間到達后觸發(fā)事件,而循環(huán)定時器就是在間隔時間到來時反復(fù)觸發(fā)事件,兩者的區(qū)別在于:前者只是作用一次,而后者則不停地作用。
比如你打開一個頁面后,想間隔幾秒自動跳轉(zhuǎn)到另一個頁面,則你就需要采用倒計定時器“setTimeout("function();",delaytime)” ,而如果想將某一句話設(shè)置成一個一個字的出現(xiàn),
則需要用到循環(huán)定時器“setInterval("function();",delaytime)” 。
獲取表單的焦點,則用到document.activeElement.id。利用if來判斷document.activeElement.id和表單的ID是否相同。
比如:if ("mid" == document.activeElement.id) {alert();},"mid"便是表單對應(yīng)的ID。
定時器:
用以指定在一段特定的時間后執(zhí)行某段程序。
JS中定時執(zhí)行,setTimeout和setInterval的區(qū)別,以及l(fā)解除方法
setTimeout(Expression,DelayTime),在DelayTime過后,將執(zhí)行一次Expression,setTimeout 運用在延遲一段時間,再進行某項操作。
setTimeout("function",time) 設(shè)置一個超時對象
setInterval(expression,delayTime),每個DelayTime,都將執(zhí)行Expression.常??捎糜谒⑿卤磉_式.
setInterval("function",time) 設(shè)置一個超時對象
SetInterval為自動重復(fù),setTimeout不會重復(fù)。
clearTimeout(對象) 清除已設(shè)置的setTimeout對象
clearInterval(對象) 清除已設(shè)置的setInterval對象
略舉兩例。
例1.表單觸發(fā)或加載時,逐字輸出字符串
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script language="JavaScript" type="text/javascript">
var str = "這個是測試用的范例文字";
var seq = 0;
var second=1000; //間隔時間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>
<body onload="setInterval('scroll()',second)">
<div id="word"></div><br/><br/>
</body>
</html>
例2.當(dāng)焦點在輸入框的時候,定時檢查輸入框信息,焦點不在時不執(zhí)行檢查動作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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; //間隔時間5秒鐘
var c=0;
function scroll() {
c++;
if ("b" == document.activeElement.id) {
var str="定時檢查第<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" style="height:100px; width:300px;" onfocus="setInterval('scroll()',second)"></textarea><br/><br/>
<div id="word"></div><br/><br/>
</body>
</html>
例3.下面這個是最簡單的例子,定時器時間到達后彈出警告窗口。
<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īng)開始!";
setTimeout("alert('十秒鐘到!')",10000)
}
</script>
<body>
<div id="m"></div>
<input TYPE="button" value=" 計時開始" onclick="count()">
</body>
</html>
例4:倒計時定時跳轉(zhuǎn)
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ds04.jsp' starting page</title>
<span id="tiao">3</span>
<a href="javascript:countDown"> </a>秒后自動跳轉(zhuǎn)……
<meta http-equiv=refresh content=3;url= '/ds02.jsp'/>
<!--腳本開始-->
<script language="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);
//下面兩個都可以用
//setTimeout("javascript:location.href='b.html'", 2000);
//setTimeout("window.location='b.html'", 2000);
</script>
例8:
<span id="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>
js定時器(執(zhí)行一次、重復(fù)執(zhí)行)
分享一段js代碼,有關(guān)js定時器的小例子,分為執(zhí)行一次的定時器與重復(fù)執(zhí)行的定時器。供初學(xué)的朋友參考。
1,只執(zhí)行一次的定時器
<script>
//定時器 異步運行
function hello(){
alert("hello");
}
//使用方法名字執(zhí)行方法
var t1 = window.setTimeout(hello,1000);
var t2 = window.setTimeout("hello()",3000);//使用字符串執(zhí)行方法
window.clearTimeout(t1);//去掉定時器
</script>
2,重復(fù)執(zhí)行的定時器
<script>
function hello(){
alert("hello");
}
//重復(fù)執(zhí)行某個方法
var t1 = window.setInterval(hello,1000);
var t2 = window.setInterval("hello()",3000);
//去掉定時器的方法
window.clearInterval(t1);
</script>
備注:
如果在一個頁面中有兩個方法,都是在頁面加載完成之后執(zhí)行的,實際卻未能按先后順序執(zhí)行,可以參照如下方法解決:
可以在onload方法中添加一個定時器,設(shè)置一個定時器,“延遲”一段時間之后再運行,即可認為區(qū)分頁面加載運行方法的先后順序。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
var YC = new Object();
function beginYC()
{
var secondsYC = document.getElementById("txtYCSeconds").value;
try
{
YC = setTimeout("alert('延遲"+secondsYC+"秒成功')",secondsYC*1000);
}
catch(e)
{
alert("請輸入正確的秒數(shù)。");
}
}
function overYC()
{
clearTimeout(YC);
YC=null;
alert("終止延遲成功。");
}
/**************************↓↓↓↓定時器的使用↓↓↓↓********************************/
var timerDS = new Object();
var timerDDS = new Object();
function beginDS()
{
sn.innerHTML = "0";
timerDS = setInterval("addOne()",parseInt(document.getElementById("txtIntervalSeconds").value,10)*1000);
}
function goonDS()
{
timerDS = setInterval("addOne()",parseInt(document.getElementById("txtIntervalSeconds").value,10)*1000);
}
function overDS()
{
clearInterval(timerDS);
timerDS=null;
}
function delayDS()
{
overDS();
timerDDS = setTimeout("goonDS()",document.getElementById("txtDDSSeconds").value*1000);
}
function addOne()
{
if(sn.innerHTML=="10")
{
overDS();
alert("恭喜你,已成功達到10秒");
return;
}
sn.innerHTML=parseInt(sn.innerHTML,10)+1;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
延遲器的使用:</div>
<div>
<label id="Label2" title="延遲秒數(shù):"></label>
<input type="text" id="txtYCSeconds" value="3" />
<input type="button" id="btnBYC" onclick="javascript:beginYC()" value="開始延遲" />
<input type="button" id="btnOYC" onclick="javascript:overYC()" value="終止延遲" />
<input type="button" id="Button1" onclick="javascript:alert('good monrning');" value="普通彈窗" />
</div>
<br />
<div>
定時器的使用:</div>
<div>
<div id="sn">0</div>
<label id="Label1" title="定時間隔秒數(shù):"></label>
<input type="text" id="txtIntervalSeconds" value="1" />
<input type="button" id="btnBDS" onclick="javascript:beginDS()" value="啟動定時" />
<input type="button" id="btnODS" onclick="javascript:overDS()" value="終止定時" />
<input type="button" id="btnGDS" onclick="javascript:goonDS()" value="繼續(xù)定時" />
<label id="ds" title="延遲秒數(shù):"></label>
<input type="text" id="txtDDSSeconds" value="5" />
<input type="button" id="btnDDS" onclick="javascript:delayDS()" value="延遲定時" />
</div>
</form>
</body>
</html>
相關(guān)文章
javascript判斷兩個IP地址是否在同一個網(wǎng)段的實現(xiàn)思路
要判斷兩個IP地址是否在同一個網(wǎng)段,將它們的IP地址分別與子網(wǎng)掩碼做與運算,得到的結(jié)果為網(wǎng)絡(luò)號,具體實現(xiàn)如下,需要的朋友可以參考下2013-12-12JavaScript 自動分號插入(JavaScript synat:auto semicolon insertion)
今天在看《Extjs中文手冊》的時候,寫了四五行樣例代碼,結(jié)果IE和Firefox一直報錯不通過。2009-11-11跟我學(xué)習(xí)javascript的函數(shù)調(diào)用和構(gòu)造函數(shù)調(diào)用
跟我學(xué)習(xí)javascript的函數(shù)和構(gòu)造函數(shù)調(diào)用,主要包括三方面內(nèi)容函數(shù)調(diào)用、方法調(diào)用以及構(gòu)造函數(shù)調(diào)用,想要了解這些內(nèi)容的朋友千萬不要錯過下面的內(nèi)容。2015-11-11Bootstrap CSS組件之面包屑導(dǎo)航(breadcrumb)
這篇文章主要為大家詳細介紹了Bootstrap CSS組件之面包屑導(dǎo)航(breadcrumb),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12typeScript?核心基礎(chǔ)之接口interface
本篇文章主要介紹?typeScript?中接口是啥?如何定義的?接口是如何進行擴展的以及類如何實現(xiàn)接口,接下來和小編一起進入下面文章一起學(xué)習(xí)?typeScript?接口2022-02-02基于javascript實現(xiàn)瀏覽器滾動條快到底部時自動加載數(shù)據(jù)
這篇文章主要介紹了基于javascript實現(xiàn)瀏覽器滾動條快到底部時自動加載數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2015-11-11Javascript+CSS實現(xiàn)影像卷簾效果思路及代碼
Arcmap里面的一個卷簾效果肯定記憶很深刻,我也對這種比較炫的卷簾效果做了一下研究,現(xiàn)在給大家匯報下結(jié)果2014-10-10