JavaScript倒数计时器每天递归调用

时间:2017-09-28 13:58:08

标签: javascript date

我正在尝试制作一个倒数计时器,显示客户购买的剩余时间,以便收到当天发货。

例如,如果他们在15:30之前购买,计时器会在30分钟内发出订单,以便今天发货(如果是15:00)。

然而,当它到达15:30时,我希望它在23小时59分钟之内说明天收到货。那么显然当它到达午夜时它将转向今天。或者它可以只显示日期/日期,所以今天/明天无关紧要。

我知道我需要再次调用该函数查看明天的日期,但我对javascript并不是很方便,所以无法弄明白。

有人可以帮忙吗?

// Set the date we're counting down to
var nowDate = new Date(); 
var countDownDate = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 15, 30, 0, 0);

// 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 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);

  // Display the result in the element with id="demo"
  if (hours >= 1) {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " + hours + "h "
  	+ minutes + "m " + seconds + "s " + "to have your order shipped on " // date of shipment;
  }
  
  else if (hours < 1 && minutes < 1) {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " + seconds + "s "
    + "to have your order shipped on " // date of shipment;
  }
  
  else {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " +  minutes + "m " 
    + seconds + "s " + "to have your order shipped on " // date of shipment;
  }
  
  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    // Start again but looking at tomorrows date
  }
  
  // If the count down is finished, write some text 
  if (nowDate.getDay() == 0 || nowDate.getDay() == 6) {
    clearInterval(x);
    document.getElementById("shipping-countdown").innerHTML = "Order within " + days + "d " 
    + hours + "h "
  	+ minutes + "m " + seconds + "s " + "to have your order shipped on " // Start of the week;
  }
}, 1000);
<!-- Display the countdown timer in an element -->
<p id="shipping-countdown"></p>

2 个答案:

答案 0 :(得分:1)

您无需清除setInterval功能。只需重置新目标日期,同时保持其活着状态。你也有一些问题,倒计时进入负面,我通过移动距离检查和重置距离,如果它低于1秒固定。

&#13;
&#13;
    // Set the date we're counting down to
    var nowDate = new Date(); 
    var countDownDate = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 11, 2, 50, 0);
    // 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;

      if (distance < 1) {
        countDownDate = countDownDate.setDate(countDownDate.getDate()+1);
        distance = countDownDate - now;
      }
      
      // Time calculations for 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);

      // Display the result in the element with id="demo"
      if (hours >= 1) {
      	document.getElementById("shipping-countdown").innerHTML = "Order within " + hours + "h "
      	+ minutes + "m " + seconds + "s " + "to have your order shipped on " // date of shipment;
      }
      
      else if (hours < 1 && minutes < 1) {
      	document.getElementById("shipping-countdown").innerHTML = "Order within " + seconds + "s "
        + "to have your order shipped on " // date of shipment;
      }
      
      else {
      	document.getElementById("shipping-countdown").innerHTML = "Order within " +  minutes + "m " 
        + seconds + "s " + "to have your order shipped on " // date of shipment;
      }
      
      // If the count down is finished, write some text 
      if (nowDate.getDay() == 0 || nowDate.getDay() == 6) {
        clearInterval(x);
        document.getElementById("shipping-countdown").innerHTML = "Order within " + days + "d " 
        + hours + "h "
      	+ minutes + "m " + seconds + "s " + "to have your order shipped on " // Start of the week;
      }
    }, 1000);
&#13;
<!-- Display the countdown timer in an element -->
<p id="shipping-countdown"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我已经设法通过以下代码实现了这一点,弄清楚我的错误行,只需调整countDownDate变量即可。

// Set the date we're counting down to
var nowDate = new Date(); 
var countDownDate = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 15, 30, 0, 0);

// 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 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);
  
    // If the count down is finished, write some text 
  if (countDownDate.getDay() == 6) {
    countDownDate.setDate(countDownDate.getDate()+2);
  }

  if (days >= 1) {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " + days + "d " + hours + "h "
  	+ minutes + "m " + seconds + "s " + "to have your order shipped on " + countDownDate;
  }
  
  else if (hours >= 1) {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " + hours + "h "
  	+ minutes + "m " + seconds + "s " + "to have your order shipped on " + countDownDate.getDate() + "/" 
    + (countDownDate.getMonth()+1) + "/" + countDownDate.getFullYear();
  }
  
  else if (minutes >= 1) {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " + minutes + "m " + seconds + "s "
     + "to have your order shipped on " + countDownDate.getDate() + "/" 
    + (countDownDate.getMonth()+1) + "/" + countDownDate.getFullYear();
  }
  
  else {
  	document.getElementById("shipping-countdown").innerHTML = "Order within " + seconds + "s "
    + "to have your order shipped on " + countDownDate.getDate() + "/" 
    + (countDownDate.getMonth()+1) + "/" + countDownDate.getFullYear();
  }
  
  // If the count down is finished
  if (distance < 0) {
    countDownDate.setDate(countDownDate.getDate()+1);
  }
  
}, 1000);
<!-- Display the countdown timer in an element -->
<p id="shipping-countdown"></p>