五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > js定時器setInterval()與setTimeout()區(qū)別10秒自動跳轉(zhuǎn)(實例講解)

js定時器setInterval()與setTimeout()區(qū)別10秒自動跳轉(zhuǎn)(實例講解)

文章來源:365jz.com     點(diǎn)擊數(shù):640    更新時間:2017-08-18 08:43   參與評論

在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秒鐘。
  倒計時定時器是在指定時間到達(dá)后觸發(fā)事件,而循環(huán)定時器就是在間隔時間到來時反復(fù)觸發(fā)事件,兩者的區(qū)別在于:前者只是作用一次,而后者則不停地作用。
比如你打開一個頁面后,想間隔幾秒自動跳轉(zhuǎn)到另一個頁面,則你就需要采用倒計定時器“setTimeout("function();",delaytime)” ,而如果想將某一句話設(shè)置成一個一個字的出現(xiàn),
則需要用到循環(huán)定時器“setInterval("function();",delaytime)” 。

獲取表單的焦點(diǎn),則用到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 運(yùn)用在延遲一段時間,再進(jìn)行某項操作。
setTimeout("function",time) 設(shè)置一個超時對象

setInterval(expression,delayTime),每個DelayTime,都將執(zhí)行Expression.常??捎糜谒⑿卤磉_(dá)式.
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)焦點(diǎn)在輸入框的時候,定時檢查輸入框信息,焦點(diǎn)不在時不執(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.下面這個是最簡單的例子,定時器時間到達(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ī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>  
//定時器 異步運(yùn)行  
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è)置一個定時器,“延遲”一段時間之后再運(yùn)行,即可認(rèn)為區(qū)分頁面加載運(yùn)行方法的先后順序。


 

<!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("恭喜你,已成功達(dá)到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>

那么問題來了。用 clearInterval(timerid);來清除,往往不能馬上停止,用什么方法比較好解決?
優(yōu)化方案如下

var timeout = false; //啟動及關(guān)閉按鈕
function time()
{
  if(timeout) return;
  Method();
  setTimeout(time,100); //time是指本身,延時遞歸調(diào)用自己,100為間隔調(diào)用時間,單位毫秒
}
總結(jié)
一般不用setInterval,而用setTimeout的延時遞歸來代替interval。
setInterval會產(chǎn)生回調(diào)堆積,特別是時間很短的時候。  
 

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評論 (640人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號