问题
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