倒计时在01:00停滞不前

时间:2016-11-15 14:37:36

标签: javascript jquery

我在jQuery中创建了一个倒计时,但我有一个问题。当计数达到function mlPushMenu( el, trigger, options ) { this.el = el; this.trigger = trigger; this.options = extend( this.defaults, options ); // support 3d transforms this.support = Modernizr.csstransforms3d; if( this.support ) { this._init(); if($(window).width() >= 991){ this._openMenu(); // added } } } 时,它会被卡住而不是以0分钟继续01:00

00:59

我的HTML。

var start = $('#start');
var countMinutes = 2;

var timer;
start.on('click', function(event) {
  event.preventDefault();
  new Timer(function(val, countMinutes) {
    timerMsg = (countMinutes >= 10 ? countMinutes : '0' + countMinutes) + ':' + (val >= 10 ? val : '0' + val);
    time.text(timerMsg);
  });
});

function Timer(callback, val, m) {
  val = val || 59;
  m = countMinutes;
  timer = setInterval(function() {
    callback(val, m);
    if (val-- <= 0) {
      m -= 1;
      if (m < 1 && val <= 0) {

        clearInterval(timer);
      }
      countMinutes = 0;
      val += 60;
    }
  }, 1000);
}

2 个答案:

答案 0 :(得分:3)

更改

if (m < 1 && val <= 0) {

if (m < 0 && val <= 0) {

答案 1 :(得分:-3)

&#13;
&#13;
var countMinutes = 2;
var timer;

$('#start').on('click', function(event) {
  event.preventDefault();
  new Timer(function(val, countMinutes) {
    timerMsg = (countMinutes >= 10 ? countMinutes : '0' + countMinutes) + ':' + (val >= 10 ? val : '0' + val);
    $('#time').text(timerMsg);
  });
});

function Timer(callback, val, m) {
  val = val || 59;
  m = countMinutes;
  timer = setInterval(function() {
    callback(val, m);
    if(val-- <= 0) {
      m -= 1;
      if(m < 0 && val <= 0) {

        clearInterval(timer);
      }
      countMinutes = 0;
      val += 60;
    }
  }, 1000);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1 id="timer">
  
<span id="time">10:00</span></h1>

<a href="#" id="start">Start</a>
&#13;
&#13;
&#13;