当秒数达到60时,我如何才能将秒数重置为0

时间:2019-06-05 17:06:39

标签: javascript timer settimeout

您好,我的网站上嵌入了秒表,但是有问题。如果秒数达到60,因此1分钟应将其重置为00。但是,在这种情况下,分钟每60秒增加一次,则秒数不会重置为零。这是我的代码

<div id="output">00:00:00</div>

<button id="startPause" onclick="startPause()">Start</button>
<button onclick="reset()">Reset</button>

<script type="text/javascript">
  var time = 0;
  var running = 0;

  function startPause() {
    if (running == 0) {
      running = 1;
      increment();
      document.getElementById('startPause').innerHTML = 'Pause';
    } else {
      running = 0;
      document.getElementById('startPause').innerHTML = 'Resume';
    }
  }

  function reset() {
    running = 0;
    time = 0;
    document.getElementById('output').innerHTML = '00:00:00';
    document.getElementById('startPause').innerHTML = 'Start';
  }

  function increment() {
    if (running == 1) {
      setTimeout(function() {
        time++;
        var mins = Math.floor(time / 10 / 60);
        if (mins <= 9) {
          mins = '0' + mins;
        }
        var secs = Math.floor(time / 10);
        if (secs <= 9) {
          secs = '0' + secs;
        }
        var tenths = Math.floor(time % 10);
        if (tenths <= 9) {
          tenths = '0' + tenths;
        }
        document.getElementById('output').innerHTML =
          mins + ':' + secs + ':' + tenths;
        increment();
      }, 100);
    }
  }
</script>

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

您可以使用该代码

<div id="output">00:00:00</div>

<button id="startPause" onclick="startPause()">Start</button>
<button onclick="reset()">Reset</button>

<script type="text/javascript">
  var time = 0;
  var running = 0;

  function startPause() {
    if (running == 0) {
      running = 1;
      increment();
      document.getElementById('startPause').innerHTML = 'Pause';
    } else {
      running = 0;
      document.getElementById('startPause').innerHTML = 'Resume';
    }
  }

  function reset() {
    running = 0;
    time = 0;
    document.getElementById('output').innerHTML = '00:00:00';
    document.getElementById('startPause').innerHTML = 'Start';
  }

  function increment() {
    if (running == 1) {
      setTimeout(function() {
        time++;
        var mins = Math.floor(time / 10 / 60);
        if (mins <= 9) {
          mins = '0' + mins;
        }
        var secs = Math.floor(time / 10);
        if(secs >= 60)
        {
         secs = secs - ( mins * 60 ); 
        }
        if ((secs).toString().length === 1) {
          secs = '0' + secs;
        }
        var tenths = Math.floor(time % 10);
        if ((tenths).toString().length === 1) {
          tenths = '0' + tenths;
        }
        document.getElementById('output').innerHTML =
          mins + ':' + secs + ':' + tenths;
        increment();
      }, 100);
    }
  }
</script>

答案 1 :(得分:0)

只需将第60行的模数乘以60,即可将秒正确重置为0(注释行以突出显示):

<div id="output">00:00:00</div>

<button id="startPause" onclick="startPause()">Start</button>
<button onclick="reset()">Reset</button>

<script type="text/javascript">
  var time = 0;
  var running = 0;

  function startPause() {
    if (running == 0) {
      running = 1;
      increment();
      document.getElementById('startPause').innerHTML = 'Pause';
    } else {
      running = 0;
      document.getElementById('startPause').innerHTML = 'Resume';
    }
  }

  function reset() {
    running = 0;
    time = 0;
    document.getElementById('output').innerHTML = '00:00:00';
    document.getElementById('startPause').innerHTML = 'Start';
  }

  function increment() {
    if (running == 1) {
      setTimeout(function() {
        time++;
        var mins = Math.floor(time / 10 / 60);
        if (mins <= 9) {
          mins = '0' + mins;
        }
        var secs = Math.floor(time / 10) % 60; // <-- see here for change
        if (secs <= 9) {
          secs = '0' + secs;
        }
        var tenths = Math.floor(time % 10);
        if (tenths <= 9) {
          tenths = '0' + tenths;
        }
        document.getElementById('output').innerHTML =
          mins + ':' + secs + ':' + tenths;
        increment();
      }, 100);
    }
  }
</script>