2

As far as I understand it, setTimeout(code, delay) will call a callback at the current time plus delay. Is there a reference clock for when the delay has expired? I.e. if I were to change the computer's current time, would setTimeout wait a different amount of time?

I'm only asking out of curiosity - not trying to use this. MDN didn't seem to have the answer.

sudo rm -rf slash
  • 1,156
  • 2
  • 16
  • 27
  • 2
    "f I were to change the computer's current time, would setTimeout wait a different amount of time?" — I suspect that would be an implementation detail of the specific JS engine and not part of the standard. – Quentin Aug 28 '23 at 17:23
  • it uses a monotonic clock, separate from the realtime clock. – Daniel A. White Aug 28 '23 at 17:23
  • 1
    @DanielA.White - got a source for that? If it's implementation dependent that might only be true for the specific browser and version you tested. – Dave S Aug 28 '23 at 17:37
  • The specification doesn't go into this detail, either. It does say that timers can be delayed. – Barmar Aug 28 '23 at 17:49
  • This is very much implementation-specific. Notice that test libraries often provide a way to mock timers ([sinon](https://sinonjs.org/releases/latest/fake-timers/), [jest](https://jestjs.io/docs/timer-mocks), [testing-library](https://testing-library.com/docs/using-fake-timers/)) which are absolutely not bound to the computer's system (wall) time. – Bergi Aug 29 '23 at 17:51

2 Answers2

1

No it would not be affected by changing computers time, but it is affected by elapsed time.

Moreover - if you setTimeout for lets say 10 seconds and then blur the tab in chrome (or e.g. close mobile Safari), all the operation will halt and the timeout will not be triggered in 10 seconds in the background. However, once the tab is focused (or the the mobile Safari is loaded again). The browser will check and automatically trigger or the timeouts that should have been triggered in the meantime, while it was sleeping.

nxtwrld
  • 1,942
  • 14
  • 15
  • 6
    Do you have any references to back this up? – Bergi Aug 28 '23 at 18:06
  • 1
    Also, your second paragraph is still true if the system clock is used. Suspending execution then waking up would check the system clock, see that time set for running the function has already passed, then trigger the function. – Dave S Aug 28 '23 at 18:11
1

So that depends...

Firstly, there is no guarantee that your timer will be executed at an exact moment in time. See both whatWG spec and NodeJS Timers docs. JS generally operates in a single thread with a loop in which things occur. If you schedule many things, things will be delayed. Furthermore, this loop can also be paused, as noted by nxtwrld.

Secondly there seem to be no standard that says exactly how to implement this. Timers are added to a global map of active timers and values in that map are "DOMHighResTimeStamp" which might be a Unix timestamp, but doesn't have to be.

https://w3c.github.io/hr-time/#dom-domhighrestimestamp

The DOMHighResTimeStamp type is used to store a duration in milliseconds. Depending on its context, it may represent the moment that is this duration after a base moment like a time origin or the Unix epoch.

So as you can see if the value is a Unix timestamp then checking the timestamp against system clock is a logical thing to do. In which case setTimeout would be affected by system clock changes.

See also:

Nux
  • 9,276
  • 5
  • 59
  • 72
  • "*if the value is a Unix timestamp then checking the timestamp against system clock is a logical thing to do*" - on the contrary. As the paragraph you quoted says, it depends on the context; in fact `performance.now()` which returns a `DOMHighResTimeStamp` does use a [monotonic clock](https://w3c.github.io/hr-time/#sec-clocks) as specified in the document you linked. However, this document says nothing about `setTimeout` – Bergi Aug 29 '23 at 17:48
  • @Bergi WWG links to the definition in their `setTimeout` specification which recomends `DOMHighResTimeStamp` as a value in a list of timers. See: https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#map-of-active-timers ... But yes, it can use either the Unix epoch or the 'moment' approach. Implementation details may vary between different vendors. – Nux Aug 30 '23 at 13:53
  • 1
    Ah, yes, the relevant part which you should quote is https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#run-steps-after-a-timeout then. And indeed, it still leaves the details that the OP is asking about open. – Bergi Aug 30 '23 at 14:38