Detecting changes to system time in JavaScript

前端 未结 7 1431
猫巷女王i
猫巷女王i 2021-01-04 04:04

How can I write a script to detect when a user changes their system time in JS?

7条回答
  •  有刺的猬
    2021-01-04 05:05

    use performance.now() to get duration, which will be independent of system clock

    see https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

    var t0 = performance.now();
    doSomething();
    var t1 = performance.now();
    console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
    

    And then you can compare performance.now() elapsed with Date.now() elapsed to see whether they are diff too much.

提交回复
热议问题