by rarestblog · 16y ago · 0 comments
Using AJAX for this would give you MUCH less accuracy than plain-old JavaScript with time delta of user time to server time:
If you just need to time sync - you could receive server time once (remembering user time, when you sent request for server time), then just compensate user time with that value.
In PSEUDO-JavaScript (client-side):
// do this once:
var user_time = (new Date()).getTime();
ajax.call('/server-time-in-sec-since-epoch',
callback:
delta_time = recvd_server_time - user_time;
)
// then at any given second real server time is:
var current_server_time = (new Date()).getTime() + delta_time;
// no need for ajax calls
For more accuracy you should divide delta_time by 2 (since it's round-trip).You could even incorporate the timer into a single request.
do {
sleep(1);
$seconds_remaining = fetch_auction_time_from_memcache();
echo "<script>updateActionTime($seconds_remaining);</script>";
flush();
} while ($seconds_remaining > 0);Longer running connections are a little more problematic, but some client side code should be able to handle the connection being closed. You just need to make sure your web server can handle the number of connections your are expecting. Apache is particularly bad for this. Something like nginx should perform better.