Clock on webpage using server and system time?

前端 未结 8 902
无人及你
无人及你 2021-01-02 03:34

I need to add a clock to a web page. The clock needs to be synchronized with a server but I really don\'t want to have it constantly check the server as the page will be ope

8条回答
  •  礼貌的吻别
    2021-01-02 04:08

    The way I've gone about this before is:

    • Take the time from the server,
    • Take the time from the client (immediately)
    • Get an offset.
    • When showing the clock, apply the offset to the current time on the client.

    You only need to update this occasionally from the server.

    The problem that you've got to sort out though is that in making a request to get the time from the server, there will be a lag before you can get the client time. You can probably minimize this by using an ajax request to get the server time and nothing else, thereby cutting overhead and network lag etc.

提交回复
热议问题