带有自动复位功能的倒数计时器

时间:2018-07-06 01:21:32

标签: javascript

我正在建立一个倒计时计时器,从现在开始三天,但是当它到达半天(6小时)时,它将自此重置为三天。

我编写了这段代码,但是我不知道如何使其自动。我尝试的几个选项是将每个负载重置为3天。现在我有一个硬编码的日期。

var countDownDate = new Date("Jul 8, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
  // Get todays date and time
  var now = new Date().getTime();
  // Find the distance between now an the count down date
  var distance = countDownDate - now;
  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  document.getElementById("timer").innerHTML = days + ":" + hours + ":"
  + minutes + ":" + seconds;

  if (distance < 0) {
  // If the count down is finished, write some text
  }
}, 1000);

0 个答案:

没有答案