-1

I have a countdown script that gets the live time and subtracts it from a set time. It all works apart from the fact that it doesn't update unless you refresh your page. The setInterval at the bottom of my function instructs the function to run every one second, but it doesn't seem to be doing that...

Can anybody help?

Here is my jsfiddle: http://jsfiddle.net/4yMZy/

Luke
  • 195
  • 2
  • 16

2 Answers2

3

Each time cCountDown runs, its is calculating the time left like so:

nDates = new Date(datetime);
xDay = new Date("Fri, 26 May 2012 16:34:00 +0000");
timeLeft = (xDay - nDates);

The value of datetime there never changes from one run to another. So cCountDown is constantly running, but is always comparing the difference between the same two dates. Since the same two dates are used, the difference is always the same, so you do not see any countdown occur.

You could change nDates = new Date(datetime); to nDates = new Date(); and it will start counting down, but I am not sure why you are getting datetime from some server in the first place.

Chris Nielsen
  • 14,731
  • 7
  • 48
  • 54
  • Still going to be stuck though, I need to use `datetime` as the countdown needs to be correct for each time zone. If someone in say America was viewing my counter and I was using `getTime` the timer would only be correct for their time zone, where as this one uses GMT. – Luke May 25 '12 at 15:58
  • I do not understand. `new Date()` always gives a date in the time zone of the user's current computer. If you want a UTC date, then just make one in JavaScript; you do not need any ajax for that: http://stackoverflow.com/a/6777470/110164 – Chris Nielsen May 25 '12 at 16:00
  • Never knew it was possible to grab the UTC date like that, thinks for this – Luke May 28 '12 at 08:05
1

There are some other issues with your code as well. You should run it through jslint or jshint.

If changed your code to http://jsfiddle.net/4yMZy/7/

So it fetches the time and updates the seconds according to the set interval.

Edit: jsfiddle actually provides a button for that on the top.

Woidda
  • 34
  • 2