我有一個JavaScript計數器:我的JavaScript計時器倒計時太快或沒有啓動的onLoad
var x=100;
function timerCountdown()
{
document.getElementById('timer1').value=x;
x--;
t=setTimeout("timerCountdown()",1000);
if (x<-1)
{
document.getElementById('timer1').value='Done!';
clearTimeout(t);
}
}
function stopCounter(){
clearTimeout(t);
x=x+1;
}
然後我用:
<body onFocus='timerCountdown()' onBlur='stopCounter()'>
但問題是,倒計時沒有按」 t在頁面加載時啓動。它等待我點擊另一個窗口並再次重新聚焦窗口。
所以,我想這一點:
<body onLoad='timerCountdown()' onFocus='timerCountdown()' onBlur='stopCounter()'>
但是這一次,倒計時進入非常快。可能是因爲timerCOuntdown每秒調用兩次。
或者,我可以在body標籤中使用onFocus和onBlur,但我需要一個函數來觸發焦點在身體負載上。那可能嗎?
有沒有人有解決這個問題的建議?
非常感謝!
什麼'<體的onLoad = 'timerCountdown()' 的onblur = 'stopCounter()'>'??爲什麼你有'onFocus ='timerCountdown()''如果你有負載? – gideon 2012-03-06 04:31:34
我不知道它在做什麼,'c - '應該會拋出一個錯誤。 – 2012-03-06 04:35:14
嗨,因爲我想定時器恢復,一旦我重新關注定時器的頁面。 – eric01 2012-03-06 04:35:29