在特定时间倒计时重置

Countdown reset on specific time

本文关键字:倒计时 定时间      更新时间:2023-09-26

目前我有一个脚本,它有一个特定日期的倒计时,但我希望倒计时在计时器上是特定的,所以假设如果我启动计时器并且我已经将计时器设置为运行 30 天,然后它将运行 30 天,然后再次重置回 30 天并开始运行。是否可以更改它来这样做?

我的代码:

    <body>
    <span id="countdown"></span>
    <script LANGUAGE="Javascript">
    // set the date we're counting down to
    var target_date = new Date("Apr 9, 2015").getTime();
    // variables for time units
    var days, hours, minutes, seconds;
    // get tag element
    var countdown = document.getElementById("countdown");
    // update the tag with id "countdown" every 1 second
    setInterval(function () {
        // find the amount of "seconds" between now and target
        var current_date = new Date().getTime();
        var seconds_left = (target_date - current_date) / 1000;
        // do some time calculations
        days = parseInt(seconds_left / 86400);
        seconds_left = seconds_left % 86400;
        hours = parseInt(seconds_left / 3600);
        seconds_left = seconds_left % 3600;
        minutes = parseInt(seconds_left / 60);
        seconds = parseInt(seconds_left % 60);
        // format countdown string + set tag value
        countdown.innerHTML = days + "d, " + hours + "h, "
        + minutes + "m, " + seconds + "s";  
    }, 1000);
</script>
</body>

编辑:

我现在已经将代码更改为如下所示,但是现在当我在浏览器中打开网站时,它是空白的。

新代码:

<span id="countdown"></span>
<script LANGUAGE="Javascript">
// set the date we're counting down to
var target_date = new Date("Apr 9, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");

if (seconds_left <= 0){
        target_date = target_date + 30 days;
    }

// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";  
}, 1000);

我真的建议您利用JavaScript库,在您的情况下,JS是完美的解决方案,您可以查看他们的文档并了解如何轻松管理时间。无论如何,这是使用时刻JS解决您的问题。

首先下载 js 并将其添加到您的页面。

网页代码

    <span id="days"> </span> 
    <span id="hours"></span>
    <span id="minutes"></span> 
    <span id="seconds"></span>

没有比这更简单的:)

JAVASCRIPT

 //create two variables for holding the date for 30 back from now using    substract
var back30Days = moment().subtract(30, 'days').format('YYYY-MM-DD H:mm:ss');
   var countDownSeconds = Math.floor(moment().diff(back30Days, 'seconds'));

 //variables holding days, hours , minutes and seconds

  var Days, Minutes, Hours, Seconds;
   // Set Interval function for performing all calculations and decrementing the countDownSeconds 
   setInterval(function () {
   // Updating Days 
   Days = pad(Math.floor(countDownSeconds / 86400), 2);
   // Updating Hours 
   Hours = pad(Math.floor((countDownSeconds - (Days * 86400)) / 3600), 2);
   // Updating Minutes
   Minutes = pad(Math.floor((countDownSeconds - (Days * 86400) - (Hours * 3600)) / 60), 2);
   // Updating Seconds
   Seconds = pad(Math.floor((countDownSeconds - (Days * 86400) - (Hours * 3600) - (Minutes * 60))), 2);
   // Updation our HTML view
   document.getElementById("days").innerHTML = Days + ' Days';
   document.getElementById("hours").innerHTML = Hours + ' Hours';
   document.getElementById("minutes").innerHTML = Minutes + ' Minutes';
   document.getElementById("seconds").innerHTML = Seconds + ' Seconds';
   // Decrement the countDownSeconds
   countDownSeconds--;
   // If we reach zero , our chrono should reset to 30 days back again, as you told
   if (countDownSeconds === 0) {
       countDownSeconds = Math.floor(moment().diff(back30Days, 'seconds'));
   }
   }, 1000);
  
 // Function for padding the seconds i.e limit it only to 2 digits
   function pad(num, size) {
       var s = num + "";
       while (s.length < size) s = "0" + s;
       return s;
   }

这是一个jsfiddle