javascript - Current time is older than the previously saved time -


my assumption time runs forward, apparently doesn’t happen way.

i have following example:

var loop_ms = 100;  var prevcall = +new date();  settimeout(function loop() {     foobar();      settimeout(loop, loop_ms); }, loop_ms);  function foobar() {     var = +new date(),         diff = - prevcall;  // expected somewhere around loop_ms      // stuff     console.log(diff);      prevcall = now; } 

now, of time works expected, , diff shows number close 100. of course, in real-life scenario i’d expect number go up, when user’s computer performing heavy operation.

what don’t expect, see negative number, , yet happens. how possible? missing something? thought maybe caused different timezone, it’s not. see in different browsers (chrome, firefox, safari), , diff values quite inconsistent: -9, -100 000.

it happens in 0.025% of cases on production, it’s not big issue, i’m wondering how on earth can possibly happen (even in theory)?

i think managed reproduce couple of times locally running script in slow vm , emulating high cpu load, still don’t see why function show negative number (meaning, now older prevcall).

most computers configured use ntp servers set system clock.

as system clock set first time stay in sync without need of updates.

at times (rarely) various reasons system clock may go out of sync.

as system clock doesn't match ntp server time updated , may go backwards.

ntp services (normally) configured avoid clock going backwards: if "gap" small enought ntp daemon tries instead "slow down" system clock until it's again in sysnc. said depends on configuration , size of time gap.

i think above cause in rare cases issue you're seeing.

you may find interesting: is there way ensure ntp synced clock never moves backwards?


if you're on macos may try opening console , searching ntpd.

on machine (a mbp 10.9.5) see couple of lines this...

apr  2 01:11:40 mac-di-paolo.local ntpd[46]: ntpd: wake time set -1.848017 s 

...that logs system clock adjustment.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -