我的countdownclock功能不断重启

时间:2017-04-26 21:42:42

标签: javascript while-loop

我似乎无法弄清楚为什么我的countdownclock功能在到达00:00后仍会重新启动。它到达00:00,然后从我在提示中输入的任何内容重新启动。我相信我需要添加条件,例如一段时间#time!=" 00:00"但我不完全确定如何。



function startTimer(duration, display) {
  var start = Date.now(),
    diff,
    minutes,
    seconds;

  function timer() {
    // get the number of seconds that have elapsed since 
    // startTimer() was called
    diff = duration - (((Date.now() - start) / 1000) | 0);

    // does the same job as parseInt truncates the float
    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
      // add one second so that the count down starts at the full duration
      // example 05:00 not 04:59
      start = Date.now() + 1000;
    }
  };

  // we don't want to wait a full second before the timer starts
  timer();
  setInterval(timer, 1000);
}

window.onload = function() {
  var choice = prompt("Minutes to launch")
  var setTheClock = 60 * choice,
    display = document.querySelector('#time');
  startTimer(setTheClock, display);
};
&#13;
<body style="width: 100vw; padding: 0; border: 0; margin: 0;">
  <div style="width: 60vh; padding: 0; border: 0; margin: 0 auto;">
    <p id="time" style="font-family:futura; font-size: 20vh; text-align: center; padding: 0; border: 0; margin-top: 40vh"></p>
  </div>
</body>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您需要清除间隔。将间隔设置为变量并在完成后将其清除,否则它将继续运行。

function startTimer(duration, display) {
  var start = Date.now(),
    diff,
    minutes,
    seconds;

  function timer() {
    var interval;
    // get the number of seconds that have elapsed since 
    // startTimer() was called
    diff = duration - (((Date.now() - start) / 1000) | 0);

    // does the same job as parseInt truncates the float
    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
      // add one second so that the count down starts at the full duration
      // example 05:00 not 04:59
      start = Date.now() + 1000;
      clearInterval(interval);
    }
  };

  // we don't want to wait a full second before the timer starts
  timer();
  interval = setInterval(timer, 1000);
}

window.onload = function() {
  var choice = prompt("Minutes to launch")
  var setTheClock = 60 * choice,
    display = document.querySelector('#time');
  startTimer(setTheClock, display);
};
<body style="width: 100vw; padding: 0; border: 0; margin: 0;">
  <div style="width: 60vh; padding: 0; border: 0; margin: 0 auto;">
    <p id="time" style="font-family:futura; font-size: 20vh; text-align: center; padding: 0; border: 0; margin-top: 40vh"></p>
  </div>
</body>

答案 1 :(得分:0)

您需要将interval存储在变量中,并在总时间达到0时将其清除。

工作解决方案。

function startTimer(duration, display) {
  var start = Date.now(),
    diff,
    minutes,
    seconds,
    countDownTimer;

  function timer() {
    // get the number of seconds that have elapsed since 
    // startTimer() was called
    diff = duration - (((Date.now() - start) / 1000) | 0);

    // does the same job as parseInt truncates the float
    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
      // add one second so that the count down starts at the full duration
      // example 05:00 not 04:59
      start = Date.now() + 1000;
	  clearInterval(countDownTimer);	
    }
  }

  // we don't want to wait a full second before the timer starts
  timer();
  countDownTimer = setInterval(timer, 1000);
}

window.onload = function() {
  var choice = prompt("Minutes to launch")
  var setTheClock = 60 * choice,
    display = document.querySelector('#time');
  startTimer(setTheClock, display);
};
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body style="width: 100vw; padding: 0; border: 0; margin: 0;">
  <div style="width: 60vh; padding: 0; border: 0; margin: 0 auto;">
    <p id="time" style="font-family:futura; font-size: 20vh; text-align: center; padding: 0; border: 0; margin-top: 40vh"></p>
  </div>
</body>
</html>