admin管理员组

文章数量:1323714

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:

/

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:

http://davedesigner.launchinhd./

Share Improve this question asked Nov 19, 2011 at 1:06 Jeff WoodJeff Wood 31 silver badge2 bronze badges 4
  • You have an excess } before the line that starts jQuery(function($) {. That makes the whole code snippet syntactically invalid. Are you getting a syntax error in the JS console? – Mike Samuel Commented Nov 19, 2011 at 1:13
  • By "not working," you mean that it displays the countdown but the countdown does not update every second? – John Kurlak Commented Nov 19, 2011 at 1:13
  • 1 @Mike... no he doesn't? That's the end of his function declaration. – John Kurlak Commented Nov 19, 2011 at 1:14
  • 1 This is offtopic, On the menu, the title of Join is empty "<a href="/join/" title="">join</a>" – ajax333221 Commented Nov 19, 2011 at 1:23
Add a ment  | 

1 Answer 1

Reset to default 9

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 countDown didn't show up for Safari, and then you would have been able to deduce that countDown was not begin called. From there, you could look at the code that calls countDown for the first time. Looking at that, you might wonder if the argument to countDown was 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.

本文标签: Javascript not working in SafariStack Overflow