尚學堂筆記 老師:馬士兵
<script language="javascript">
var leftTime=5000;
function go(){
document.getElementById("num").innerText=leftTime;
leftTime-=1000;
if(leftTime<=0){
document.location.href="cart.jsp";
}
}
setInterval(go,1000);
</script>
setInterval() 方法可按照指定的周期(以毫秒計)來調用函數或計算表達式。
setInterval() 方法會不停地調用函數,直到 clearInterval() 被調用或窗口被關閉。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數。
setInterval(code,millisec[,"lang"])
| 參數 | 描述 |
|---|---|
| code | 必需。要調用的函數或要執行的代碼串。 |
| millisec | 必須。周期性執行或調用 code 之間的時間間隔,以毫秒計。 |
一個可以傳遞給 Window.clearInterval() 從而取消對 code 的周期性執行的值。
<html><body><input type="text" id="clock" size="35" /><script language=Javascript>var int=self.setInterval("clock()",50)function clock() { var t=new Date() document.getElementById("clock").value=t }</script></form><button onclick="int=window.clearInterval(int)">Stop interval</button></body></html>定義和用法
clearInterval() 方法可取消由 setInterval() 設置的 timeout。
clearInterval() 方法的參數必須是由 setInterval() 返回的 ID 值。
語法
clearInterval(id_of_setinterval)
| 參數 | 描述 |
|---|---|
| id_of_setinterval | 由 setInterval() 返回的 ID 值。 |
下面這個例子將每隔 50 毫秒調用 clock() 函數。您也可以使用一個按鈕來停止這個 clock:
<html><body><input type="text" id="clock" size="35" /><script language=javascript>var int=self.setInterval("clock()",50)function clock() { var t=new Date() document.getElementById("clock").value=t }</script></form><button onclick="int=window.clearInterval(int)">Stop interval</button></body></html>
新聞熱點
疑難解答