參考網站:
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/http://www.w3schools.com/jsref/obj_window.asp
W3schools請找Window Object Methods的部份
Javascript的計時功能提供了setTimeout與setInterval這兩種用法,當然也有取消計時功能的函數
分別是clearTimeout()和clearInterval()。
而setTimeout與setInterval最大的差異就是setTimeout只有計時一次,而setInterval是會一直重複
計時直到你用了clearInterval()去取消他。
1.setTimeoutandclearTimeout
setTimeout ( expression, timeout );clearTimeout ( timeoutId );
Example :var timeout=0;
function callme()
{
alert("Hello Timeout!!");
}
window.onload = function() {
timeoutId = setTimeout("callme()",2000);
}
callme函數將會在2秒後被呼叫,timeout的單位為微秒(milliseconds)
如果你突然想在2秒內去取消計時的功能的話就要用到clearTimeout這個功能了
以下的範例利用按鈕來取消Timeout的計時功能:
< input type="button" name="clickMe" value="Click me to cancel!"
onclick="alertTimerClickHandler();">
function alertTimerClickHandler ( ){
alert("Cancel timeout");
clearTimeout ( timeoutId );
}
2. set…
留言