【问题标题】:Count down timer, with a dynamic date倒数计时器,带有动态日期
【发布时间】:2017-02-13 09:40:49
【问题描述】:

我正在编写一个倒数计时器,它会在每天早上 11 点重置,我打电话的日期是动态的,意味着日期每天都会改变。 有些我是如何正确编码的,它有时会显示正确的倒计时,但当我在 Windows 系统上随机检查它时,它通常会显示 NAN NAN。

下面是我的代码-

<div class="countdown">
    <span>DEAL TIME LEFT : </span>
    <p id="demo"></p>
</div>
<script>

    var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    //alert(year);
    // Set the date we're counting down to
    var countDownDate = new Date(day+" "+month+" "+year+",  11:00:00").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 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("demo").innerHTML = hours + " : "
    + minutes + " : " + seconds;

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

即使我检查了,alert(day),alert(month), alert(year)所有这些都是正确的,但我仍然收到 “NAN,NAN”错误.

【问题讨论】:

    标签: javascript html magento-1.9


    【解决方案1】:

    这里有问题

    var countDownDate = new Date(day+" "+month+" "+year+",  11:00:00").getTime();
    

    Date 构造函数无法解析您的字符串并且.getTime() 返回NaN

    而不是使用字符串。可以使用Date构造函数的其他参数。

    新日期(年,月[,日期[,小时[,分钟[,秒[, 毫秒]]]]]);

    var countDownDate = new Date(year, month, day,11).getTime();
    

    <div class="countdown">
    <span>DEAL TIME LEFT : </span>
    <p id="demo"></p>
    
    </div>
    <script>
    
    var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    var day = currentDate.getDate();
    var month = currentDate.getMonth() ;
    var year = currentDate.getFullYear();
     //alert(year);
    // Set the date we're counting down to
    
    var countDownDate = new Date(year, month, day,11).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 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("demo").innerHTML = hours + " : "
        + minutes + " : " + seconds;
    
        // If the count down is over, write some text 
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("demo").innerHTML = "EXPIRED";
        }
    }, 1000);
    </script>

    【讨论】:

    • 那会给出错误的月份,你还有var month = currentDate.getMonth() + 1;。你看不到它,因为你只看时间。 ;-)
    • @RobG - 请详细说明,我不明白我哪里还有错?
    • @Khushbu_sipl — 这将创建一个日期,获取月份,加 1,然后使用 +1 月份创建另一个日期。它可能会显示正确的时间,因为月份不是结果的一部分,但它仍然会创建不正确的日期。我已经发布了一个答案,更多的是作为如何去做的一个例子,而不是回答你的具体问题。
    【解决方案2】:

    我猜你想倒数到上午 11 点,如果是在 11 点之后,则到明天上午 11 点。

    以下创建的结束日期为今天 11:00,或者如果在此之后,则为明天 11:00。然后格式化剩余时间。然后有一个计时器每秒调用一次,就在下一整秒之后,这样它就不会漂移。

    它只创建一个日期并显示如何添加一天并将其设置为特定时间。

    希望 cmets 足够了。

    function showRemaining() {
      var endDate = new Date();
    
      // If after 1100, add a day
      if (endDate.getHours() > 11) {   
        endDate.setDate(endDate.getDate() + 1);
      }
      // Set time to 11am;
      endDate.setHours(11,0,0,0);
    
      // Get seconds from now to end
      var diff = Math.ceil((endDate - Date.now())/1000);
    
      // Show time as h:mm:ss
      return ( (diff/3.6e3|0) + ':' +
               ('0'+((diff%3.6e3)/60|0)).slice(-2) + ':' +
               ('0'+(diff%60)).slice(-2)
             );
    }
     
    // Run every second just after next full second
    (function timer() {
        console.log(showRemaining());
        var lag = 1020 - (Date.now()%1000)
        setTimeout(timer, lag);
    }());

    【讨论】:

    • 感谢 RobG :) 让您快速回放
    猜你喜欢
    • 2021-02-03
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多