Sync JS time between multiple devices

后端 未结 5 874
忘了有多久
忘了有多久 2020-11-29 03:23

I\'m using the wonderful reveal.js library to create a HTML slideshow. My only problem is that I need it to synchronise across multiple devices.

At the moment I am m

5条回答
  •  [愿得一人]
    2020-11-29 04:14

    I'm glad you found a satisfactory answer to your question. I had a similar need to synchronize the browser with the server's clock and was determined to achieve it with better than 1 second accuracy like you were. I've written code to do this and am posting this answer here in case anyone else needs the solution too.

    The code is called ServerDate and is freely available for download. Here's part of the README. Notice that I achieved 108 ms precision in my example:

    You can use ServerDate as you would use the Date function or one of its instances, e.g.:

    > ServerDate()
    "Mon Aug 13 2012 20:26:34 GMT-0300 (ART)"
    
    > ServerDate.now()
    1344900478753
    
    > ServerDate.getMilliseconds()
    22
    

    There is also a new method to get the precision of ServerDate's estimate of the server's clock (in milliseconds):

    > ServerDate.toLocaleString() + " ± " + ServerDate.getPrecision() + " ms"
    "Tue Aug 14 01:01:49 2012 ± 108 ms"
    

    You can see the difference between the server's clock and the browsers clock, in milliseconds:

    > ServerDate - new Date()
    39
    

提交回复
热议问题