首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示不同系统上的倒计时计时器差异

显示不同系统上的倒计时计时器差异
EN

Stack Overflow用户
提问于 2017-11-28 15:47:02
回答 0查看 32关注 0票数 1

我正在尝试用javascript做一个倒计时计时器,我写了一个代码,如下所示

代码语言:javascript
运行
复制
var countdown = function(){ 
    setInterval(function() {
        var countDownDate = new Date(document.getElementById("end_date").getAttribute("data-date")).getTime(); 
        // data-date ex. = "2017-11-28 21:54:00"; greater than current date (now)
        // 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"
        if(hours<10){
           hours = "0"+hours;
        }
        if(minutes<10){
           minutes = "0"+minutes;
        }
        if(seconds<10){
           seconds = "0"+seconds;
        }
        var left = hours + ":"+ minutes + ":" + seconds;
        console.log(left);
        document.getElementById("time_left").innerHTML = left;

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

计数器工作得很好,但是为什么我在不同的系统上得到了长达25秒的时间差。有些系统显示相同的倒计时时间,但有些则不是。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47526455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档