Javascript 跳过秒

Javascript skips seconds

我是javascript的新手,我想在它执行指定功能之前创建一个 6 小时倒数计时器,但时间显示完全错误

我哪里做错了,如何才能正确显示小时、分钟和秒?

这是代码

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 * 360) | 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 fiveMinutes = 60 * 2,
    display = document.querySelector('#time');
  startTimer(fiveMinutes, display);
};
<span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account

<!DOCTYPE html>
<html>

<head></head>

<body>
  <div id='time'></div>

  <script type="text/javascript">
    function startTimer(duration, display) {

      setInterval(function() {
        duration = duration - 1;
        if (duration == -1) {
          clearInterval(duration);
          return;
        }

        var seconds = duration % 60;
        var minutes = Math.floor(duration / 60);
        var hours = Math.floor(minutes / 60);
        minutes %= 60;
        hours %= 60;

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

      }, 1000);
    }

    window.onload = function() {
      var sixhours = 60 * 60 * 6
      display = document.querySelector('#time');
      startTimer(sixhours, display);
    };
  </script>
</body>

</html>

您正在计算每次执行的分钟数。仅在秒数达到 0.

时才计算分钟

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
    if(!minutes) minutes = (diff / 60 * 360) | 0
    seconds = (diff % 60) | 0;
    minutes = seconds<=0?(minutes-1) : minutes;

    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 fiveMinutes = 60 * 2,
    display = document.querySelector('#time');
  startTimer(fiveMinutes, display);
};
<span id="time" style="background:white;border:2px solid green;color:green;paddding:5px;"></span> To Delete Your Account