![](/img/trans.png)
[英]How to stop timer after ten seconds in javascript using setInterval and clearInterval?
[英]Javascript : setInterval Timer when stop using clearInterval can't restart it again
我正在使用小時秒分鍾計時器。 我使用setInterval啟動它。 但是點擊按鈕重新啟動計時器后,它將不再起作用。 該按鈕應該清除計時器(clearInterval),然后重新啟動它。
我嘗試在clearInterval之后調用包含計時器的函數,但是計時器仍然無法正常工作。
計時器代碼
function gameTimer(intervalTime){
var input = {
hours: 0,
minutes: 0,
seconds: 0
};
var timestamp = new Date(input.hours, input.minutes, input.seconds);
var interval = intervalTime;
setInterval(function () {
timestamp = new Date(timestamp.getTime() + interval * 1000);
document.querySelector('.countdown2').innerHTML = timestamp.getHours() + 'h:'
+ timestamp.getMinutes() + 'm:' + timestamp.getSeconds() + 's';
}, Math.abs(interval) * 1000);
}
gameTimer(1);
重置功能
function resetFunc(){
clearInterval(gameTimer(0))
}
let reset = document.getElementById('restart');
reset.addEventListener('click', resetFunc);
此功能后,我再次調用計時器功能以再次重新啟動計時器,但是它不起作用
gameTimer(1);
您沒有從gameTimer
函數return
。 為了訂閱setInterval
,您需要從中獲取值。 所以代碼會去:
function gameTimer(intervalTime){
var input = {
hours: 0,
minutes: 0,
seconds: 0
};
var timestamp = new Date(input.hours, input.minutes, input.seconds);
var interval = intervalTime;
// here!!
return setInterval(function () {
timestamp = new Date(timestamp.getTime() + interval * 1000);
document.querySelector('.countdown2').innerHTML = timestamp.getHours() + 'h:'
+ timestamp.getMinutes() + 'm:' + timestamp.getSeconds() + 's';
}, Math.abs(interval) * 1000);
}
而且您的resetFunc
也有問題:
function resetFunc(){
clearInterval(gameTimer(0))
}
這將啟動計時器,但同時清除gameTimer
返回的間隔。 因此,從本質上講,它什么也不做。
因此,完整的答案將如下所示:
<html>
<head>
<script>
window.onload = function(){
let isTimerRunning = false
let intervalRef
function gameTimer(intervalTime){
var input = {
hours: 0,
minutes: 0,
seconds: 0
};
var timestamp = new Date(input.hours, input.minutes, input.seconds);
var interval = intervalTime;
// here!!
return setInterval(function () {
console.log('hey')
}, Math.abs(interval) * 1000);
}
function toggleTimer(interval){
if (isTimerRunning){
isTimerRunning = false
clearInterval(intervalRef)
}
else{
isTimerRunning = true
intervalRef = gameTimer(interval)
}
}
let reset = document.getElementById('restart');
reset.addEventListener('click', function(){toggleTimer(1)});
}
</script>
</head>
<body>
<button id = 'restart'>
toggle
</button>
</body>
</html>
請注意,我已經簡化了setInterval
的回調邏輯。
var interval, intervalTime2 = 1; function gameTimer(intervalTime){ if (interval){ clearInterval(interval); }; intervalTime2 = intervalTime; var input = { hours: 0, minutes: 0, seconds: 0 }; var timestamp = new Date(input.hours, input.minutes, input.seconds); interval = setInterval(function () { timestamp = new Date(timestamp.getTime() + intervalTime * 1000); document.querySelector('.countdown2').innerHTML = timestamp.getHours() + 'h:' + timestamp.getMinutes() + 'm:' + timestamp.getSeconds() + 's'; }, Math.abs(intervalTime) * 1000); } gameTimer(1); function resetFunc(){ gameTimer(intervalTime2); } let reset = document.getElementById('restart'); reset.addEventListener('click', resetFunc);
<span class="countdown2"></span> <button id="restart">Restart</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.