window對(duì)象
window對(duì)象是BOM的核心,window對(duì)象指當(dāng)前的瀏覽器窗口。
window對(duì)象方法:

計(jì)時(shí)器
在JavaScript中,我們可以在設(shè)定的時(shí)間間隔之后來(lái)執(zhí)行代碼,而不是在函數(shù)被調(diào)用后立即執(zhí)行。
計(jì)時(shí)器類(lèi)型:
一次性計(jì)時(shí)器:僅在指定的延遲時(shí)間之后觸發(fā)一次。
間隔性觸發(fā)計(jì)時(shí)器:每隔一定的時(shí)間間隔就觸發(fā)一次。
計(jì)時(shí)器方法:

計(jì)時(shí)器setInterval()
在執(zhí)行時(shí),從載入頁(yè)面后每隔指定的時(shí)間執(zhí)行代碼。
語(yǔ)法:
setInterval(代碼,交互時(shí)間);
參數(shù)說(shuō)明:
代碼:要調(diào)用的函數(shù)或要執(zhí)行的代碼串。
交互時(shí)間:周期性執(zhí)行或調(diào)用表達(dá)式之間的時(shí)間間隔,以毫秒計(jì)(1s=1000ms)。
返回值:
一個(gè)可以傳遞給 clearInterval() 從而取消對(duì)"代碼"的周期性執(zhí)行的值。
調(diào)用函數(shù)格式(假設(shè)有一個(gè)clock()函數(shù)):
setInterval("clock()",1000)
或
setInterval(clock,1000)
我們?cè)O(shè)置一個(gè)計(jì)時(shí)器,每隔100毫秒調(diào)用clock()函數(shù),并將時(shí)間顯示出來(lái),代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
var int=setInterval(clock, 100)
function clock(){
var time=new Date();
document.getElementById("clock").value = time;
}
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
取消計(jì)時(shí)器clearInterval()
clearInterval() 方法可取消由 setInterval() 設(shè)置的交互時(shí)間。
語(yǔ)法:
clearInterval(id_of_setInterval)
參數(shù)說(shuō)明:
id_of_setInterval:由 setInterval() 返回的 ID 值。
每隔 100 毫秒調(diào)用 clock() 函數(shù),并顯示時(shí)間。當(dāng)點(diǎn)擊按鈕時(shí),停止時(shí)間,代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>計(jì)時(shí)器</title>
<script type="text/javascript">
function clock(){
var time=new Date();
document.getElementById("clock").value = time;
}
// 每隔100毫秒調(diào)用clock函數(shù),并將返回值賦值給i
var i=setInterval("clock()",100);
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
<input type="button" value="Stop" onclick="clearInterval(i)" />
</form>
</body>
</html>
計(jì)時(shí)器setTimeout()
setTimeout()計(jì)時(shí)器,在載入后延遲指定時(shí)間后,去執(zhí)行一次表達(dá)式,僅執(zhí)行一次。
語(yǔ)法:
setTimeout(代碼,延遲時(shí)間);
參數(shù)說(shuō)明:
- 要調(diào)用的函數(shù)或要執(zhí)行的代碼串。
- 延時(shí)時(shí)間:在執(zhí)行代碼前需等待的時(shí)間,以毫秒為單位(1s=1000ms)。
當(dāng)我們打開(kāi)網(wǎng)頁(yè)3秒后,在彈出一個(gè)提示框,代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
setTimeout("alert('Hello!')", 3000 );
</script>
</head>
<body>
</body>
</html>
當(dāng)按鈕start被點(diǎn)擊時(shí),setTimeout()調(diào)用函數(shù),在5秒后彈出一個(gè)提示框。
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function tinfo(){
var t=setTimeout("alert('Hello!')",5000);
}
</script>
</head>
<body>
<form>
<input type="button" value="start" onClick="tinfo()">
</form>
</body>
</html>
要?jiǎng)?chuàng)建一個(gè)運(yùn)行于無(wú)窮循環(huán)中的計(jì)數(shù)器,我們需要編寫(xiě)一個(gè)函數(shù)來(lái)調(diào)用其自身。在下面的代碼,當(dāng)按鈕被點(diǎn)擊后,輸入域便從0開(kāi)始計(jì)數(shù)。
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var num=0;
function numCount(){
document.getElementById('txt').value=num;
num=num+1;
setTimeout("numCount()",1000);
}
</script>
</head>
<body>
<form>
<input type="text" id="txt" />
<input type="button" value="Start" onClick="numCount()" />
</form>
</body>
</html>
取消計(jì)時(shí)器clearTimeout()
setTimeout()和clearTimeout()一起使用,停止計(jì)時(shí)器。
語(yǔ)法:
clearTimeout(id_of_setTimeout)
參數(shù)說(shuō)明:
id_of_setTimeout:由 setTimeout() 返回的 ID 值。該值標(biāo)識(shí)要取消的延遲執(zhí)行代碼塊。
下面的例子和上節(jié)的無(wú)窮循環(huán)的例子相似。唯一不同是,現(xiàn)在我們添加了一個(gè) "Stop" 按鈕來(lái)停止這個(gè)計(jì)數(shù)器:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var num=0,i;
function timedCount(){
document.getElementById('txt').value=num;
num=num+1;
i=setTimeout(timedCount,1000);
}
setTimeout(timedCount,1000);
function stopCount(){
clearTimeout(i);
}
</script>
</head>
<body>
<form>
<input type="text" id="txt">
<input type="button" value="Stop" onClick="stopCount()">
</form>
</body>
</html>