本文整理了一份關於 JavaScript 定時器的實戰筆記,涵蓋「只執行一次」與「重複執行」兩類定時器,並附上多個可直接運行的示例,需要的朋友可以參考。
一、只執行一次的定時器
<script>
//定時器 異步運行
function hello(){
alert("hello");
}
//使用方法名字執行方法
var t1 = window.setTimeout(hello,1000);
var t2 = window.setTimeout("hello()",3000);//使用字符串執行方法
window.clearTimeout(t1);//去掉定時器
</script>
二、重複執行的定時器
<script>
function hello(){
alert("hello");
}
//重復執行某個方法
var t1 = window.setInterval(hello,1000);
var t2 = window.setInterval("hello()",3000);
//去掉定時器的方法
window.clearInterval(t1);
</script>
核心概念
JavaScript 中有兩個定時器專用函數:
1. 倒計時定時器:timename = setTimeout("function();", delaytime);
2. 循環定時器:timename = setInterval("function();", delaytime);
第一個參數是要執行的動作,可以是一個函數,也可以是多個用分號隔開的函數;第二個參數 delaytime 是間隔時間,單位為毫秒,例如填寫 5000 即代表 5 秒。倒計時定時器到達指定時間後只觸發一次,而循環定時器會在每個間隔週期反覆觸發。
舉例來說,若希望在打開頁面後每隔幾秒自動跳轉到另一個頁面,就應該採用 setTimeout;若想讓一句話逐字出現,則需要用到 setInterval。
補充一個小技巧:如果一個頁面裡有兩個都在加載完成後執行的函數,卻無法保證先後順序,可以在 onload 中借助定時器「延遲」一段時間執行,藉此區分執行先後。
此外,若想取得當前獲得焦點的表單元素,可使用 document.activeElement.id,再用 if 判斷其 id 是否與目標表單一致,例如:if ("mid" == document.activeElement.id) { alert(); }。
解除定時器的方法
clearTimeout(對象) 用於清除 setTimeout 設置的定時器;
clearInterval(對象) 用於清除 setInterval 設置的定時器。
示例一:逐字輸出字符串
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</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.getElementById('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>
示例二:焦點在輸入框時定時檢查
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</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.getElementById('b').value!=""){
str+="輸入框當前內容為<br/><b> "+document.getElementById('b').value+"</b>";
}
document.getElementById('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>
示例三:定時彈出警告窗口
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="javascript">
function count() {
document.getElementById('m').innerHTML="計時已經開始!";
setTimeout("alert('十秒鐘到!')",10000)
}
</script>
<body>
<div id="m"></div>
<input TYPE="button" value=" 計時開始" onclick="count()">
</body>
</html>
示例四:倒計時定時跳轉
<html>
<head>
<title>My JSP 'ds04.jsp' starting page</title>
<span id="tiao">3</span>秒後自動跳轉……
<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>
</head>
</html>
示例五:meta 實現跳轉
<head>
<meta http-equiv="refresh" content="2;url='b.html'">
</head>
示例六:setTimeout 修改地址跳轉
<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>
示例七:結合倒數與頁面跳轉
<span id="totalSecond">2</span>
<script language="javascript" type="text/javascript">
var second = document.getElementById('totalSecond').innerHTML;
if(isNaN(second)){
//不是數字的處理方法
}else{
setInterval(function(){
document.getElementById('totalSecond').innerHTML = --second;
if (second <= 0) {
window.location = 'b.html';
}
}, 1000);
}
</script>