Get Server time to the browser
I am getting timestamp from开发者_如何学Python the server using javascript/ajax. Now once I have this I should be able to compute the server time now on using the timestamp. I dont want to refer the client time. because that can be changed. Any suggestion. Thanks in advance.
You don't say what your accuracy/resolution requirements are but, in general, you can't rely on Javascript timers (see here).
I would agree that you can't guarantee that the local clock is accurate at any point in time. However, it's pretty unlikely that a user will change the clock in any particular period.
You could get the time from the server and store the difference between it and the local time. You could then use a timer to re-check the difference periodically (once a minute should be more than enough) and adjust your offset.
Whenever you need to make a time-dependent decision, just apply the difference.
You will, of course have to adjust for the local timezone but you could make this easier by configuring your server to send UTC and just work in UTC in the browser, converting whenever you need to display a time.
精彩评论