2

I have an application which displays countdown of time left for user to perform his action. Time is not per task, but to actual timestamp.

For example, user must do simple action before 17:58:32 UTC+0 today.

I am getting action and finishing time from server and then proceed to display countdown. I create empty Date() object (to get current users time), substract it from finishing time, and display countdown.

But here is where problem kicks in. If users computer time is off by significant value, this difference is added. Finishing time from server is synchronized with universal time. Current time, drawn on users machine may be not.

First thing to think about is to send current time from server too. But then it will not be exactly current. By the moment it is used 1, 2 even 3 seconds may pass after it was generated on server. Slow internet connection, clogged javascript execution stack (plugins, userscripts), etc. And in my application seconds matter.

It there anything that can be done to get actual, synchronized with universal values time in javascript?

Nameless
  • 2,306
  • 4
  • 23
  • 28

1 Answers1

1

You will never get them exact, but I suspect that after making a few requests to the server for the current time, and getting the average offset from the client clock, you can get close.

As you have pointed out, this method isn't flawless, but is as probably as close as you can get.

Brad
  • 159,648
  • 54
  • 349
  • 530