whatwg / whatwg/html

Should setInterval account for execution delays to prevent drift?

Open
#3,151 49 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

interop
Dominant language
HTML
Stars
9.4k
Forks
3.2k
PR merge metrics
PR metrics pending

Description

If I am reading the 'timer initialisation steps' correctly, a timer set with setInterval should (after the delay) invoke the passed function then schedule another callback with the same delay. However, I'm not aware of any browser that actually does this:

  • Firefox appears to schedule the next callback before invoking the function, or at least subtracts the execution time from the next delay.
  • Chrome and Edge seem to go one step further, also reducing the delay for the next callback if the start of the current invocation was late. This means some intervals may be lower than the specified delay in order to maintain it as an average. (Look for values <10 here: test.)

(Note: I'm ignoring the behaviour of background tabs and cases where the function takes longer to execute than the delay.)

In my opinion the spec should be updated to describe the behaviour of Chrome and Edge, which is clearly the intuitive behaviour for developers. Whilst the spec already notes that it is wrong to rely on the timers for precision, which will still remain the case, it should be possible to rely on setInterval not to cause application-level drift in the order of minutes when the delay is as low as 1s.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the HTML Standard's timer initialisation steps and compare them with the linked Mozilla test and the browser behavior described. Review the 49-comment thread for unresolved consensus; done means an agreed specification change with corresponding timer wording and tests identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
html
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.