I have a count down script, but I have 2 issues. 1. my countdown never reaches 0 and stops so i get a continous negative number. 2. the counter only displays in chrome and not firefox or internet explorer.How do I fix these two isses?
var sec, min, hr, day, timer, expireDate;
sec = 1000;
min = 60 * 1000;
hr = 60 * 60 * 1000;
day = 24 * 60 * 60 * 1000;
timer;
expireDate = new Date('Mon Sep 17 2012 14:26:00 GMT-0700');
timer = setInterval(function() {
var currentDate, countdown, days, hours, minutes, seconds;
currentDate = new Date();
countdown = expireDate - currentDate;
if (countdown === 0 ) {
window.clearInterval(timer);
document.getElementById('countdown').innerHTML = 'EXPIRED!';
}
days = Math.floor(countdown / day);
hours = Math.floor((countdown % day) / hr);
minutes = Math.floor((countdown % hr) / min);
seconds = Math.floor((countdown % min) / sec);
console.log(countdown);
document.getElementById('countdown').innerHTML = days + " " + hours + " " + minutes + " " + seconds;
}, 1000);
As stated by others, you should be using
< 0.In addition, once your expired condition is met, you are immediately overwriting the
EXPIRED!label, so you will never see it. You will need to move the code following theifinto anelseor simply return within theif.And finally, the reason it wont’ work in IE is likely the
console.log. IE will fail on this if you do not have a console open at the time. Simply remove theconsole.logline and this works just fine in IE.