我的计时器计数不正确

时间:2017-08-03 09:26:12

标签: jquery

目前正致力于一个简单的番茄钟,但我遇到了一个无法解决的问题。当您按下开始按钮时,计时器会倒计时,但是,它会在00:01而不是00:00中更改模式。有人可以偷看并提出导致此错误的原因吗?

  var flag = false;
  var timer;
  var work = 2;
  var count = 0;
  var breakTime = 3;
  var workTime = 4;
  var active = 'break';

  $('.mins').html(updateNum(work));
  $('.secs').html(updateNum(count));
  $('.session-minutes').html(work);

function workTimer() {

  if(count < 0) {
    work--;
    count = 2;
  }
  else if (work == 0 && count == 0) {

    if(active === 'work') {
      $(".timer-header").html('Work Time!');
      work = workTime;
      $('.mins').html(updateNum(work));
      return active = 'break';
    }
    if (active === 'break') {
      $(".timer-header").html('Break Time!');
      console.log(active);
      work = breakTime;
      $('.mins').html(updateNum(work));
      return active = 'work';
    }
  }

  $('.mins').html(updateNum(work));
  $('.secs').html(updateNum(count));
  count--;
}
$('.start-button').click(function() {
  if (flag == false) {

    timer=setInterval(workTimer,1000);
    $(this).html("stop");
    flag = true;
    return flag;
  }
  else if(flag == true){

      clearInterval(timer);
      $(this).html("start");
      flag = false;
      return flag;
  }
})

$('.reset-button').click(function() {
  if(flag == false) {
    work = 10;
    count = updateNum(0);
    $('.mins').html(work);
    $('.secs').html(count);
    clearInterval(timer);
  }

})

$('.session-plus').click(function() {
  if(flag == false) {
    if (work < 30) {
      $('.mins').html(updateNum(++work));
      $('.session-minutes').html(work);
    }

  }

})

$('.session-minus').click(function() {
  if(flag == false) {
    if (work > 1) {
      $('.mins').html(updateNum(--work));
      $('.session-minutes').html(work);
    }
  }

})

function updateNum(num) {
  if(num < 10 && num.length != 2) {
      return num = "0" + num;
  }
  else {
      return num;
  }

}

https://jsfiddle.net/6vnkgt3h/

1 个答案:

答案 0 :(得分:0)

问题在于:

Scanner()

正确的是:if(count < 0) { work--; count = 2; }

工作演示here