JavaScript倒计时器应该停止在00:00。

3
我正在处理倒计时器,我找到了这段代码,它基本上是我所需要的,但我不想在到达 00:00 时重新启动计时器。我尝试进行更改,但没有成功。
以下是 JsFiddle 代码:链接
<div id="countdown">1:00</div>

var seconds;
var temp;
var GivenTime=document.getElementById('countdown').innerHTML
console.log(GivenTime)

function countdown() {
time = document.getElementById('countdown').innerHTML;
timeArray = time.split(':')
seconds = timeToSeconds(timeArray);
if (seconds == '') {
temp = document.getElementById('countdown');
temp.innerHTML = GivenTime;
    time = document.getElementById('countdown').innerHTML;
timeArray = time.split(':')
seconds = timeToSeconds(timeArray);
}
seconds--;
    temp = document.getElementById('countdown');
temp.innerHTML= secondsToTime(seconds);
timeoutMyOswego = setTimeout(countdown, 1000);
    

}
function timeToSeconds(timeArray) {  
var minutes = (timeArray[0] * 1);
var seconds = (minutes * 60) + (timeArray[1] * 1);
return seconds;
}

function secondsToTime(secs) {
var hours = Math.floor(secs / (60 * 60));
hours = hours < 10 ? '0' + hours : hours;
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
minutes = minutes < 10 ? '0' + minutes : minutes;
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
seconds = seconds < 10 ? '0' + seconds : seconds;
    
  
return  minutes + ':' + seconds;
//hours + ':' + 
    
}
countdown();
2个回答

3

当倒计时达到00:00时,您可以简单地使用clearTimeout函数。

只需在您的倒计时中添加此if条件即可。

if (secondsToTime(seconds) == '00:00') {
   clearTimeout(timeoutMyOswego); //stop timer
}

工作演示:

var seconds;
var temp;
var GivenTime = document.getElementById('countdown').innerHTML

function countdown() {
  time = document.getElementById('countdown').innerHTML;
  timeArray = time.split(':')
  seconds = timeToSeconds(timeArray);
  if (seconds == '') {
    temp = document.getElementById('countdown');
    temp.innerHTML = GivenTime;
    time = document.getElementById('countdown').innerHTML;
    timeArray = time.split(':')
    seconds = timeToSeconds(timeArray);
  }
  seconds--;
  temp = document.getElementById('countdown');
  temp.innerHTML = secondsToTime(seconds);
  var timeoutMyOswego = setTimeout(countdown, 1000);
  if (secondsToTime(seconds) == '00:00') {
    clearTimeout(timeoutMyOswego); //stop timer
    console.log('Time"s UP')
  }
}

function timeToSeconds(timeArray) {
  var minutes = (timeArray[0] * 1);
  var seconds = (minutes * 60) + (timeArray[1] * 1);
  return seconds;
}

function secondsToTime(secs) {
  var hours = Math.floor(secs / (60 * 60));
  hours = hours < 10 ? '0' + hours : hours;
  var divisor_for_minutes = secs % (60 * 60);
  var minutes = Math.floor(divisor_for_minutes / 60);
  minutes = minutes < 10 ? '0' + minutes : minutes;
  var divisor_for_seconds = divisor_for_minutes % 60;
  var seconds = Math.ceil(divisor_for_seconds);
  seconds = seconds < 10 ? '0' + seconds : seconds;

  return minutes + ':' + seconds;
  //hours + ':' + 

}
countdown();
<div id="countdown">00:10</div>


1
你可以尝试替换负责重新启动计时器的代码。
但仅此而已会导致计时器变为负值。
因此,正如@Always Helping所建议的那样,你需要添加停止计时器的代码。

var seconds, temp;
var GivenTime = document.getElementById('countdown').innerHTML;

function countdown() {
  time = document.getElementById('countdown').innerHTML;
  timeArray = time.split(':')
  seconds = timeToSeconds(timeArray);
  console.log(seconds);
  
  // Do something when the timer is out
  if (seconds === 0) {
    clearTimeout(timeoutMyOswego);
    return;
  }
  
  seconds--;
  temp = document.getElementById('countdown');
  temp.innerHTML = secondsToTime(seconds);
  timeoutMyOswego = setTimeout(countdown, 1000);
};
countdown();

function timeToSeconds(timeArray) {
  var minutes = (timeArray[0] * 1);
  var seconds = (minutes * 60) + (timeArray[1] * 1);
  return seconds;
}

function secondsToTime(secs) {
  var hours = Math.floor(secs / (60 * 60));
  hours = hours < 10 ? '0' + hours : hours;
  var divisor_for_minutes = secs % (60 * 60);
  var minutes = Math.floor(divisor_for_minutes / 60);
  minutes = minutes < 10 ? '0' + minutes : minutes;
  var divisor_for_seconds = divisor_for_minutes % 60;
  var seconds = Math.ceil(divisor_for_seconds);
  seconds = seconds < 10 ? '0' + seconds : seconds;

  return minutes + ':' + seconds;
}
<div id="countdown">00:05</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接