I have a jQuery countdown script written, it works in all other browsers except Safari.
function countdown(secondsRemaining) {
secondsRemaining = Math.floor(secondsRemaining);
var days = Math.floor(secondsRemaining / 86400),
hours = Math.floor((secondsRemaining - (days * 86400)) / 3600),
minutes = Math.floor((secondsRemaining - (days * 86400) - (hours * 3600)) / 60),
seconds = secondsRemaining - (days * 86400) - (hours * 3600) - (minutes * 60);
if(secondsRemaining > 0) {
if(days < 10) { days = '0' + days; }
if(hours < 10) { hours = '0' + hours; }
if(minutes < 10) { minutes = '0' + minutes; }
if(seconds < 10) { seconds = '0' + seconds; }
jQuery('#countdown .days').html(days);
jQuery('#countdown .hours').html(hours);
jQuery('#countdown .minutes').html(minutes);
secondsRemaining--;
}
window.setTimeout(function() {
countdown(secondsRemaining);
}, 1000);
}
jQuery(function($) {
countdown(Math.floor((Date.parse('2012-02-15') - new Date().getTime())/1000));
});
You can see it in action here:
The problem is with:
Math.floor((Date.parse('2012-02-15') - new Date().getTime())/1000)It returns NaN (not a number) in Safari.
Try using:
countdown(Math.floor((Date.parse('Feb 15, 2012') - new Date().getTime())/1000));Alternatively, you can try something like:
countdown(Math.floor((new Date(2012, 1, 15, 0, 0, 0, 0).getTime() - new Date().getTime())/1000));In the future, you can debug things like this by putting debug/alert/console messages throughout your code. You would have seen that your debug message in
countDowndidn’t show up for Safari, and then you would have been able to deduce thatcountDownwas not begin called. From there, you could look at the code that callscountDownfor the first time. Looking at that, you might wonder if the argument tocountDownwas wrong, so maybe you’d try to print that out. In Safari, you’d see NaN, so then you’d print out each individual part until you saw the problem. From there, you would be that it didn’t like Date.parse(), so maybe you’d read up on Date.parse() to see if you were giving it the right input.