1

I am executing simple javascript function with setInterval which just print date & time

console.log('before',new Date().toLocaleString(), new Date().getMilliseconds())
setInterval(() => {
    console.log(new Date().toLocaleString(), new Date().getMilliseconds());
}, 1000);

output of code is

before 7/27/2023, 9:06:14 PM 364
7/27/2023, 9:06:15 PM 367
7/27/2023, 9:06:16 PM 370
7/27/2023, 9:06:17 PM 372
7/27/2023, 9:06:18 PM 376
7/27/2023, 9:06:19 PM 378
7/27/2023, 9:06:20 PM 382

my question is why does it always add some milliseconds on every execution of code, okay I know that it take some jitter milliseconds to actually add function to call stack then it should not always add jitter milliseconds to the previous function call.

why output would not look like this ?

before 7/27/2023, 9:06:14 PM 364
7/27/2023, 9:06:15 PM 367
7/27/2023, 9:06:16 PM 367
7/27/2023, 9:06:17 PM 367
7/27/2023, 9:06:18 PM 368
7/27/2023, 9:06:19 PM 367
7/27/2023, 9:06:20 PM 367
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 3
    setTimeout is just not accurate, you can't rely on it for precise timeing – kevinSpaceyIsKeyserSöze Jul 27 '23 at 16:18
  • 1
    setInterval defines an interval it which a callback routine is added to the event loop. It does not guarantee the callback is executed immediately, as there can be other events in the loop that are queued beforehand. – h0r53 Jul 27 '23 at 16:20
  • 1
    @Mark the question is not about why `setInterval` (or `setTimeout`) is inaccurate, but rather why the error accumulates for `setInterval` instead of being corrected for drift. – Bergi Jul 27 '23 at 16:31
  • 1
    I don't see it accumulating, it goes up and down for me. – Barmar Jul 27 '23 at 16:34
  • In my last attempt I got 499, 501, 500, 500, 500. – Barmar Jul 27 '23 at 16:34
  • Notably, for me this accumulates in Node v16, but not in Chrome. – user137794 Jul 27 '23 at 16:35
  • 1
    I don't think the specification is precise about this, so the actual result is likely to be implementation-dependent. – Barmar Jul 27 '23 at 16:39
  • 1
    Related: [Will setInterval drift?](https://stackoverflow.com/questions/985670/will-setinterval-drift), [Can setInterval drift over time?](https://stackoverflow.com/questions/18167059/can-setinterval-drift-over-time), [what does the standard say](https://stackoverflow.com/q/10739835/1048572) - but you're neither asking *whether* it will drift nor *how* to [correct for that](https://stackoverflow.com/q/29971898/1048572), rather *why* it is drifting, in nodejs specifically - right? – Bergi Jul 27 '23 at 16:39
  • @user137794 (and @Jemish): which OS are you running on? – Bergi Jul 27 '23 at 16:40
  • I'm on Chrome / Linux and it doesn't seem to drift. eg. `403 403 403 900 403 403 403 500 403` etc. IOW: I't seems to try and keep at 403. – Keith Jul 27 '23 at 16:42
  • 2
    Is there any particular case for requiring it not to drift?, because I would also like to point out that browsers these days also implement timer throttling that will muddy the waters even more.. – Keith Jul 27 '23 at 16:46
  • yes, I forgot to mention that I am running this code in node js environment – Jemish Virani Jul 28 '23 at 04:04

0 Answers0