Domanda

I'm working on a site for a client where he needs a countdown timer to count down to 10pm two days from each site update. (or however many days he wishes to enter) The countdown functions perfectly in Google Chrome but in Internet Explorer and Firefox it shows NaN NaN. I've read this means "not a number" so is there something wrong with the way my code in calculating the date information? Any help would be appreciated! My code is below:

<span id="countdown1" class="countdown"></span>
     <script language="javascript">

     // set the date we're counting down to 2 days from now at 10pm
var target_date1 = Date.parse("2014-01-05 22:00:00:000");

// variables for time units
var days1, hours1, minutes1, seconds1;

// get tag element
var countdown1 = document.getElementById("countdown1");

// update the tag with id "countdown" every 1 second
setInterval(function () {

// find the amount of "seconds" between now and target
var current_date1 = new Date().getTime();
var seconds_left1 = (target_date1 - current_date1) / 1000;

// do some time calculations
days1 = parseInt(seconds_left1 / 86400);
seconds_left1 = seconds_left1 % 86400;

hours1 = parseInt(seconds_left1 / 3600);
seconds_left1 = seconds_left1 % 3600;

minutes1 = parseInt(seconds_left1 / 60);
seconds1 = parseInt(seconds_left1 % 60);

// format countdown string + set tag value
countdown1.innerHTML = days1 + " days, " + hours1 + " hours, "
+ minutes1 + " minutes, " + seconds1 + " seconds";  

}, 1000);
</script>
È stato utile?

Soluzione

Your date format won't work in Firefox (and apparently IE). This:

Date.parse("2014-01-05 22:00:00:000")

is NaN in Firefox. Firefox likes dates like "Dec 31, 2013" or ISO dates like "2011-10-10T14:48:00". More information at MDN.

Altri suggerimenti

Use a RFC2822 or ISO 8601 date format for what you pass to Date.parse() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

check this out you might like it as a count down.. wrote is yesterday:

http://jsfiddle.net/sf7Fc/ It will put your count down in a selector. you could change the way the time appear.

countDown($('#selector'), 300);
<div id="selector"></div>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top