计时器不会在00:00停止

时间:2015-07-08 15:15:48

标签: javascript timer countdown

我的倒数计时器不会停止00:00它会继续前进,直到它达到“-01:59”然后“-02:59”等等,我在这里和那里改变了一些代码,但它仍然继续去

optgroup

1 个答案:

答案 0 :(得分:2)

这里是:应该将setTimeout分配给变量,当secs为0时,清除超时。



     // set minutes
    var mins = 1;

     // calculate the seconds (don't change this! unless time progresses at a      different speed for you...)
    var secs = mins * 10;
    var timeout;

    function countdown() {
      timeout = setTimeout('Decrement()', 1000);
    }

    function Decrement() {
      if (document.getElementById) {
        minutes = document.getElementById("minutes");
        seconds = document.getElementById("seconds");
        // if less than a minute remaining
        if (seconds < 59) {
          seconds.value = secs;
        } else {
          minutes.value = getminutes();
          seconds.value = getseconds();
        }
        secs--;
        if (secs < 0) {
          clearTimeout(timeout);
          return;
        }
        countdown();
      }
    }

    function getminutes() {
      // minutes is seconds divided by 60, rounded down
      mins = Math.floor(secs / 60);
      return ("0" + mins).substr(-2);
    }

    function getseconds() {
      // take mins remaining (as seconds) away from total seconds remaining
      return ("0" + (secs - Math.round(mins * 60))).substr(-2);
    }
    countdown();
&#13;
<div id="timer">This is only valid for the next
  <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">:
  <input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
&#13;
&#13;
&#13;

希望这有帮助。