Skip to content

Editorial: Use the HTML spec's timers infrastructure for timeout - #98

Open
andreubotella wants to merge 1 commit into
w3c:gh-pagesfrom
andreubotella:run-steps-after-timeout
Open

Editorial: Use the HTML spec's timers infrastructure for timeout#98
andreubotella wants to merge 1 commit into
w3c:gh-pagesfrom
andreubotella:run-steps-after-timeout

Conversation

@andreubotella

@andreubotella andreubotella commented Dec 2, 2021

Copy link
Copy Markdown
Member

In whatwg/html#7349, the HTML spec factored out part of the infrastructure for setTimeout() into a "run steps after a timeout" algorithm for use by other specs. This change updates requestIdleCallback() to use that algorithm when the timeout property is present and positive.


Preview | Diff

In whatwg/html#7349, the HTML spec factored out part of the
infrastructure for `setTimeout()` into a "run steps after a timeout"
algorithm for use by other specs. This change updates
`requestIdleCallback()` to use that algorithm when the `timeout`
property is present and positive.
@w3cbot

w3cbot commented Dec 2, 2021

Copy link
Copy Markdown

andreubotella marked as non substantive for IPR from ash-nazg.

@yoavweiss yoavweiss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for aligning this with HTML spec changes.

@noamr - can you review?

Comment thread index.html
@andreubotella

andreubotella commented Dec 2, 2021

Copy link
Copy Markdown
Member Author

Come to think about it, this change is not in fact editorial, since the "run steps after a timeout" algorithm adds an entry to the map of active timers, which in turn affects the idle deadline. It might still be worth making the change, though.

@noamr noamr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yoavweiss

Copy link
Copy Markdown
Contributor

@andreubotella - For some reason, we never clicked the green button and merged this.. Any reason I shouldn't? :)

@mmocny

mmocny commented Jun 23, 2026

Copy link
Copy Markdown

Issue triage: @shaseley can you check if this still makes sense to land, please? Seems it was meant to be landed a few years back.

@shaseley

Copy link
Copy Markdown
Collaborator

As @andreubotella mentioned, this actually changes behavior in an observable way since the idle deadline computation is based on queued timers. But looking at Chrome's implementation, we do take into account idle task timeouts in the deadline computation, albeit with a few differences [1] (which ought to be fixed). Looks like FF also takes pending idle timeouts into account.

So I think this PR actually matches existing behavior, but we might not want the timer associated with an idle task that runs while idle (didn't time out yet) to be considered. Since the timer isn't removed from the map when its corresponding idle task runs, it counts towards the deadline:

setTimeout(() => {
  // If this ran while idle with no frame expected, timeRemaining should be ~50, not ~10.
  // You can see the difference in Chrome and FF if changing the timeout to 10000.
  requestIdleCallback(d => {
    console.log('Time remaining: ' + d.timeRemaining());
  }, {timeout: 10});
}, 1000);

We can get around that by removing the timer from the map before executing the callback.

Note: Chrome's does not currently remove the timer from the map, it does that after the callback runs (it's also static and fixed before running the callback, so it would take some work to fix this). It looks like FF matches Chrome's behavior here as well, as we have the same behavior with that snippet. cc @smaug---- for thoughts on the FF side.

[1] Chrome doesn't support dynamically added timers, and for some reason, we only take the pending timers into account for "long idle periods", i.e. when we aren't expecting a frame. This might predate some of the spec work that integrated the deadline computation into HTML a few years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants