JavaScript NTP time

后端 未结 3 1838
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 01:50

I\'m writing a counting script who counts the time between an old date and today.
Everything worked good until I tested on a computer with wrong date and saw the resul

3条回答
  •  独厮守ぢ
    2020-12-04 02:17

    First of all, the JS scheduler has a certain granularity - that is, you can request an interval smaller than, say, 20 msec, but it will not fire immediately - what you could see is 20 events fired off every 20 msec.

    Second, even if you could, this is not a good idea: you would be making 1000 requests every second, from every computer which uses this script. Even if the client and their connections could handle this, it's nothing short of a DDoS for the JSON server.

    What you could do is this:

    • get time from JSON-NTP (once), this will be a Date
    • get local time (once), this will be a Date
    • calculate the difference between NTP and local time (once), this will likely be the number of msec that local time is off
    • for every time calculation, take the difference into account

提交回复
热议问题