Skip to content

Purpose of timer nesting level for repeating timers is unclear #239

Closed

Description

@jdm

I'm reading through https://html.spec.whatwg.org/multipage/webappapis.html#timer-initialisation-steps to review an implementation in Servo and having a hard time understanding the role of the timer nesting level in regards to repeating timers. In particular, it looks like any any call to setTimeout or setInterval will yield a task with a timer nesting value of 1; however, given that a repeating timer automatically calls the timer initialization steps from inside the task, this looks like the timer nesting level for said repeating timer will monotonically increase each time the timer repeats. Is that actually the behaviour that is desired?

Activity

  1. wanderview commented on Oct 8, 2015

    @wanderview
    Member

    Isn't the purpose of this to implement minimum interval latching? See step 7 of the section you linked:

    "If nesting level is greater than 5, and timeout is less than 4, then increase timeout to 4."

    Basically to prevent:

    function foobar() {
      setTimeout(foobar, 0);
    }
    foobar();
    
  2. jdm commented on Oct 8, 2015

    @jdm
    MemberAuthor

    Yes, I understand how it works for the non-setInterval case, but I don't understand what is supposed to happen for repeating timers.

  3. zcorpan commented on Oct 9, 2015

    @zcorpan
    Member

    From what I understand of the spec, it should be basically equivalent to nest setTimeout as to use setInterval, but it's not clear it is implemented like that in all browsers. Check the following test:

    data:text/html,<script>onclick = function() { var last = new Date(); var times = []; var i = 0; var interval = setInterval(function() { var now = new Date(); times.push(now - last); last = now; i++; if (i == 20) { clearInterval(interval); document.body.innerHTML = times.join(','); } }, 0); }; </script>Click to run test
    

    In WebKit I get 2,1,1,2,1,5,5,5,5,6,5,5,4,5,5,5,5,5,6,5 which suggests it either doesn't clamp for the first 5, or maybe it clamps to 1ms for the first 5.

    In Blink I get 1,1,1,1,4,4,5,4,4,4,4,4,4,4,4,4,4,4,4,4 which suggests it either doesn't clamp for the first 4, or maybe it clamps to 1ms for the first 4.

    In Gecko I get 5,4,4,3,4,5,4,4,4,4,4,3,5,4,4,4,4,4,4,4 which suggests it clamps to 4ms from the start.

    In Presto I get 4,4,4,4,4,5,4,4,4,4,4,4,4,4,4,4,4,4,5,4 which suggests it clamps to 4ms from the start.

    Haven't tested IE/Edge.

    For comparison, here's the equivalent test for setTimeout:

    data:text/html,<script>onclick = function() { var last = new Date(); var times = []; var i = 0; setTimeout(function f() { var now = new Date(); times.push(now - last); last = now; i++; if (i == 20) { document.body.innerHTML = times.join(','); } else { setTimeout(f, 0); } }, 0); }; </script>Click to run test
    

    WebKit: 1,2,1,2,1,5,5,5,6,5,4,5,4,5,4,6,5,4,6,5

    Blink: 1,1,2,1,5,5,5,6,5,5,5,5,4,5,5,6,5,5,5,6

    Gecko: 1,0,0,0,5,6,4,5,5,6,5,5,6,5,6,5,5,6,4,6

    Presto: 4,4,5,4,4,4,4,4,4,4,5,4,4,4,4,4,4,4,4,5

  4. domenic commented on May 19, 2016

    @domenic
    Member

    @jdm does this still seem unclear? Do you have any suggestions for making it clearer? Maybe some examples showing the desired timings implied by the requirements here, like in @zcorpan's post?

  5. jdm commented on May 20, 2016

    @jdm
    MemberAuthor

    As written, both nested setTimeout and setInterval have the unusual behaviour of overflowing the nesting level at some point unless interrupted. That was what I found confusing, especially because the nesting level is treated as a threshold and its actual value doesn't matter at any other time.

  6. annevk commented on May 20, 2016

    @annevk
    Member

    @jdm specification integers have no bounds though. See https://html.spec.whatwg.org/multipage/infrastructure.html#conformance-classes "User agents may impose implementation-specific limits on otherwise unconstrained inputs, e.g. to prevent denial of service attacks, to guard against running out of memory, or to work around platform-specific limitations."

  7. jdm commented on May 20, 2016

    @jdm
    MemberAuthor

    Actually, there is a practical implication to this conflation of repeated timers and nesting:

    x = 0;
    setInterval(function() {
      x++;
      if (x == 5) {
        setTimeout(function() { ... }, 0);
      }
    }, 1000);

    It's counterintuitive to me that the setTimeout is expected to be clamped to a minimum time greater than 0, given the specification text.

  8. domenic commented on May 20, 2016

    @domenic
    Member

    Do you have any concrete suggestions for ways we could make it less confusing for you?

  9. zcorpan commented on May 20, 2016

    @zcorpan
    Member
  10. domenic commented on May 20, 2016

    @domenic
    Member

    Right, it seems likely that this is one of the cases where the spec is trying to make the best of a non-interoperable situation. So I'm not sure the spec's normative content should change. I'm curious how @jdm thinks clarity could be improved, however.

  11. jdm commented on May 20, 2016

    @jdm
    MemberAuthor

    If the behaviour that's written is intentional, which it appears to be, I don't have any clear suggestions. My initial reading struck me as a possible oversight given the conflation between nesting and non-nested behaviours. If we either rename the things related to nesting to encompass repeating timers as well, or add a note explaining that repeated timers are intentionally treated equivalently to nested ones, that would satisfy me.

  12. added
    clarification
    on May 23, 2016
  13. added a commit that references this issue on Jun 1, 2016
    ba17920
  14. added a commit that references this issue on Jun 6, 2016
    e37a80d
  15. lrozenblyum commented on May 26, 2021

    @lrozenblyum

    Hi!
    Sorry for raising this old issue up, however even after the spec. clarification it's a little bit unclear about the simple case:

    Does it mean that such a simple code
    setInterval(function() { ... }, 1000)
    increases nesting level at each next invocation of the function scheduled?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    clarification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions