0

I'm developing a site that will have 8 timers, each ending between 30 seconds or a minute apart. The timers endtime will be set server side and sent to the page on page load. I am using Pusher.com to manage other parts of the page (using live events)

Every person that loads the page needs to see the same countdown time no matter where they are. That is why I have chosen to provide the users browser with an endtime, and the counters would be calculated using that end time.

The endtime would be given in UTC timezone. How would I go about creating a countdown timer that checks a server for the current UTC time and displays a countdown to that endtime?

I'm quite new to javascript as I mainly do backend stuff, so it's a bit of a mind-freeze when it comes to trying to do this across many users around the world.

Any help would be appreciated!

KriiV
  • 1,882
  • 4
  • 25
  • 43
  • 1
    Your solution would rely on the accuracy of client's clock. Instead you could have specified the delta to the end time. – zerkms Aug 11 '15 at 06:01
  • Would that be a good way to go about it? Wouldn't the client be a few seconds out between the setting up of the clock etc? So you're saying pass the client the delta to end time and get the JS to simply use that to countdown to 0? – KriiV Aug 11 '15 at 06:03
  • How do you know it's not minutes or hours? Well, yes, for the task as you explained it I'd go with just delta. – zerkms Aug 11 '15 at 06:03
  • The countdowns will start at between 3 and 5 mins. This is global, the countdowns aren't started on page load. The server basically sets the countdown (using Laravel(PHP)) and whenever someone joins, it tags onto that and displays a countdown – KriiV Aug 11 '15 at 06:04
  • Like I said, I'm new to JS so I'm probably confusing myself. – KriiV Aug 11 '15 at 06:05
  • JavaScript Date objects use a UTC time value and the same epoch as UNIX. The only difference is that JS uses milliseconds not seconds, see [*Convert a Unix timestamp to time in Javascript*](http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript). You can then read back UTC values using [*getUTCHours*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-date.prototype.getutchours), etc. Of course synchronisation will be a challenge. ;-) – RobG Aug 11 '15 at 06:11

0 Answers0