兩個關於定時器的專用函數
倒計定時器:timename=setTimeout("function();",delaytime);
循環定時器:timename=setInterval("function();",delaytime);
函數中的參數解釋
第一個參數"function()"是定時器觸發時要執行的動做,能夠是一個函數,也能夠是幾個函數,函數間用";"隔開便可。
好比要彈出兩個警告窗口,即可將"function();"換成"alert('第一個警告窗口!');alert('第二個警告窗口!')";
而第二個參數"delaytime"則是間隔的時間,以毫秒爲單位,即填寫"5000",就表示5秒鐘。
- 倒計時定時器是在指定時間到達後觸發事件
- 循環定時器就是在間隔時間到來時反覆觸發事件,
- 二者的區別:前者只是做用一次,然後者則不停地做用。
解除定時器的函數
clearTimeout(對象) 清除已設置的setTimeout對象
clearInterval(對象) 清除已設置的setInterval對象
循環定時器的例子
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="javascript">
function count() {
setInterval("alert('2秒鐘到!')",2000)
}
</script>
<body>
<div id="m"></div>
<input TYPE="button" value=" 計時開始" onclick="count()">
</body>
</html>
倒計時定時器的例子
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<script language="javascript">
function count() {
setTimeout("alert('2秒鐘到!')", 2000)
}
</script>
<body>
<div id="m"></div>
<input TYPE="button" value=" 計時開始" onclick="count()">
</body>
</html>