JQuery countdown timer delayed

点点圈 提交于 2020-01-07 00:07:15

问题


I have implemented the JQuery Countdown Timer (jquery.lwtCountdown-1.0.js). Am facing the delay in the timer in different machines and same machine different browsers. Sometimes it take 40-50 seconds delay in total 15 mins. The javascript logic is defined below:

<script type="text/javascript" >
    var Timer;

    function CreateTimer(TimerID, Time){
            Timer = document.getElementById(TimerID);
    TotalSeconds = Time;

    UpdateTimer();
    window.setTimeout("Tick()", 1000);
}
function Tick() {
    TotalSeconds -= 1;
    if(TotalSeconds>=0){
        UpdateTimer();
        window.setTimeout("Tick()", 1000);
    }else{
            // NO TIME THEN LOGOUT
                    //          alert(SUCCESS_LOGOUT);
    }
}
function UpdateTimer() {
        var Seconds = TotalSeconds;

    var Days = Math.floor(Seconds / 86400);
    Seconds -= Days * 86400;

    var Hours = Math.floor(Seconds / 3600);
    Seconds -= Hours * (3600);

    var Minutes = Math.floor(Seconds / 60);
    Seconds -= Minutes * (60);


    var TimeStr = "" +((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)+"<b>"
    Timer.innerHTML = TimeStr;
}

function LeadingZero(Time) {
    return (Time < 10) ? "0" + Time : + Time;
}

</script>

This above code is of javascript. I can't find any resolution for timer getting delayed. Is there anything am missing?

Thanks in advance!!!


回答1:


There is two issues with your code:

1. window.setTimeout("Tick()", 1000); here you are passing a string you need to 
                                      pass  a function 
2. you need a global variable for TotalSeconds

i'm not sure what this code does, however here is a working DEMO



来源:https://stackoverflow.com/questions/23659661/jquery-countdown-timer-delayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!