You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Purpose of timer nesting level for repeating timers is unclear #239
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?
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
@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?
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.
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.
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.
Activity
wanderview commentedon Oct 8, 2015
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:
jdm commentedon Oct 8, 2015
Yes, I understand how it works for the non-
setIntervalcase, but I don't understand what is supposed to happen for repeating timers.zcorpan commentedon Oct 9, 2015
From what I understand of the spec, it should be basically equivalent to nest
setTimeoutas to usesetInterval, but it's not clear it is implemented like that in all browsers. Check the following 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: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
domenic commentedon May 19, 2016
@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?
jdm commentedon May 20, 2016
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.
annevk commentedon May 20, 2016
@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."
jdm commentedon May 20, 2016
Actually, there is a practical implication to this conflation of repeated timers and nesting:
It's counterintuitive to me that the setTimeout is expected to be clamped to a minimum time greater than 0, given the specification text.
domenic commentedon May 20, 2016
Do you have any concrete suggestions for ways we could make it less confusing for you?
zcorpan commentedon May 20, 2016
Maybe browsers don't match the spec here...
http://software.hixie.ch/utilities/js/live-dom-viewer/saved/4222
Both Gecko and Chrome don't clamp when the nested
setTimeoutstarts. But Safari seems to do that.Now with 0 interval for the outer
setInterval:http://software.hixie.ch/utilities/js/live-dom-viewer/saved/4223
Here Chrome suddenly clamps the
setTimeout.domenic commentedon May 20, 2016
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.
jdm commentedon May 20, 2016
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.
Clarify the purpose of timer nesting level
Clarify the purpose of timer nesting level
lrozenblyum commentedon May 26, 2021
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?