On 1 Kwi, 14:30, Hans Peter <[email protected]> wrote:
> Hi,
> im trying to build a accurate countdown clock. are there any best
> practices you could recommend?
>
> Is it even possible to have a 100% accurate timer? (server latency
> etc..)
>
> my approach would be to get the time from the server on page load and
> sync it again after mabye one minute.
>
> thanks for the input
>
> pete

You should never depend on setTimeout, it's inaccurate.
When I wrote a clock (countdown, actually) I was using getUTC*
functions of Date object. You can call updater every 1000ms, get user
time and calculate correct server time. Even if updater won't fire
after 1000ms, it doesn't matter. You can call it even more frequently
(like 500ms) to avoid skipping second if setTimeout takes longer than
it should.
If you want to be more accurate, send timestamp (with first load) from
server and calculate difference to local time, then add it with every
update.
Request to server always take some time (both ajax and initial http),
so I would prefer relying on user time more than server time. Ajax
sync may cause clock to adjust in very strange way, user could assume
it is broken.

Cheers,
Sebastian Poręba

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to