倒数计时器到特定时间

时间:2017-03-22 08:23:40

标签: javascript

我有一个日期倒数计时器的Javascript代码。我想在网上商店订购流程中使用此代码。但是,我不想每天手动编辑日期。我希望计时器每天计算到相同的目标时间。



// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 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);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = "Bestellen Sie innerhalb " + hours + "Stunden und " +
    minutes + "Minuten " + "und wir versenden noch am gleichen Tag!";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
&#13;
<div id="timer">
  <!-- Display the countdown timer in an element -->
  <p id="demo"></p>
</div>
&#13;
&#13;
&#13;

有人有解决方案吗?

2 个答案:

答案 0 :(得分:0)

以下代码段会将当前时间增加1小时,并将其用作countDownDate

&#13;
&#13;
// Set the date we're counting down to
var countDownDate = new Date();
countDownDate.setHours(countDownDate.getHours() + 1);
countDownDate = countDownDate.getTime()

// Update the count down every 1 second
var x = setInterval(function() {
  // Get todays date and time
  var now = Date.now();

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

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = "Bestellen Sie innerhalb " + hours + "Stunden und " +
    minutes + "Minuten " + "und wir versenden noch am gleichen Tag!";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
&#13;
<div id="timer">
  <!-- Display the countdown timer in an element -->
  <p id="demo"></p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

    // Get today's date and time
    var now = new Date().getTime();

    // Find the distance between now and the target 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);

    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";

    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
</script>

结果如下:

  

289d 4h 41m 8s