Skip to content

Commit 6949e88

Browse files
committed
Review feedback; move example
1 parent 66af549 commit 6949e88

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

source

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96435,8 +96435,8 @@ enum <dfn enum>DOMParserSupportedType</dfn> {
9643596435
<p class="note">For entries put in the <span>map of active timers</span> by the <span>timer
9643696436
initialization steps</span>, i.e., by <code data-x="dom-setTimeout">setTimeout()</code> and <code
9643796437
data-x="dom-setInterval">setInterval()</code>, the keys are numbers. For other specifications
96438-
that use the <span>wait for a timeout</span> algorithm, the identifier is a unique non-numeric
96439-
value. Only the numeric-keyed timers are affected by <code
96438+
that use the <span>run steps after a timeout</span> algorithm, the identifier is a unique
96439+
non-numeric value. Only the numeric-keyed timers are affected by <code
9644096440
data-x="dom-clearTimeout">clearTimeout()</code> and <code
9644196441
data-x="dom-clearInterval">clearInterval()</code>, but all timers contribute to <a
9644296442
href="#idle-deadline-computation">idle deadline computation</a>, and are cleared when the
@@ -96483,8 +96483,8 @@ enum <dfn enum>DOMParserSupportedType</dfn> {
9648396483

9648496484
<li><p>If <var>previousId</var> was given, let <var>id</var> be <var>previousId</var>;
9648596485
otherwise, let <var>id</var> be an <span>implementation-defined</span> integer that is greater
96486-
than zero that will identify the timeout to be set by this call in <var>global</var>'s <span>map
96487-
of active timers</span>.</p></li>
96486+
than zero and does not already <span data-x="map exists">exist</span> in <var>global</var>'s
96487+
<span>map of active timers</span>.</p></li>
9648896488

9648996489
<li>
9649096490
<p>If the <span>surrounding agent</span>'s <span data-x="concept-agent-event-loop">event
@@ -96594,14 +96594,14 @@ enum <dfn enum>DOMParserSupportedType</dfn> {
9659496594
<var>global</var> to run <var>task</var>.</p></li>
9659596595

9659696596
<li>
96597-
<p><span>Wait for a timeout</span> given <var>global</var>, "<code
96597+
<p><span>Run steps after a timeout</span> given <var>global</var>, "<code
9659896598
data-x="">setTimeout/setInterval</code>", <var>timeout</var>, <var>completionStep</var>, and
96599-
<var>handle</var>.</p>
96599+
<var>id</var>.</p>
9660096600

9660196601
<p class="note">Once the task has been processed, if <var>repeat</var> is false, it is safe to
96602-
remove the entry for <var>handle</var> from the <span>map of active timers</span> (there is no
96603-
way for the entry's existence to be detected past this point, so it does not technically matter
96604-
one way or the other).</p>
96602+
remove the entry for <var>id</var> from the <span>map of active timers</span> (there is no way
96603+
for the entry's existence to be detected past this point, so it does not technically matter one
96604+
way or the other).</p>
9660596605
</li>
9660696606

9660796607
<li><p>Return <var>id</var>.</p></li>
@@ -96624,7 +96624,38 @@ setTimeout({ toString: function () {
9662496624
} }, 100);</code></pre>
9662596625
</div>
9662696626

96627-
<p>To <dfn export>wait for a timeout</dfn>, given a <code>WindowOrWorkerGlobalScope</code>
96627+
</div>
96628+
96629+
<div class="example">
96630+
<p>To run tasks of several milliseconds back to back without any delay, while still yielding back
96631+
to the browser to avoid starving the user interface (and to avoid the browser killing the script
96632+
for hogging the CPU), simply queue the next timer before performing work:</p>
96633+
96634+
<pre><code class="js">function doExpensiveWork() {
96635+
var done = false;
96636+
// ...
96637+
// this part of the function takes up to five milliseconds
96638+
// set done to true if we're done
96639+
// ...
96640+
return done;
96641+
}
96642+
96643+
function rescheduleWork() {
96644+
var id = setTimeout(rescheduleWork, 0); // preschedule next iteration
96645+
if (doExpensiveWork())
96646+
clearTimeout(id); // clear the timeout if we don't need it
96647+
}
96648+
96649+
function scheduleWork() {
96650+
setTimeout(rescheduleWork, 0);
96651+
}
96652+
96653+
scheduleWork(); // queues a task to do lots of work</code></pre>
96654+
</div>
96655+
96656+
<div w-nodev>
96657+
96658+
<p>To <dfn export>run steps after a timeout</dfn>, given a <code>WindowOrWorkerGlobalScope</code>
9662896659
<var>global</var>, a string <var>orderingIdentifier</var>, a number <var>milliseconds</var>, a
9662996660
set of steps <var>completionSteps</var>, and an optional value <var>timerKey</var>:</p>
9663096661

@@ -96633,7 +96664,8 @@ setTimeout({ toString: function () {
9663396664
<span>timer initialization steps</span>. (Other specifications must not pass
9663496665
<var>timerKey</var>.)</p></li>
9663596666

96636-
<li><p>If <var>timerKey</var> is not given, set it to a new unique non-numeric value.</p></li>
96667+
<li><p>If <var>timerKey</var> is not given, then set it to a new unique non-numeric
96668+
value.</p></li>
9663796669

9663896670
<li><p>Let <var>startTime</var> be the <span>current high resolution time</span>.</p></li>
9663996671

@@ -96675,40 +96707,14 @@ setTimeout({ toString: function () {
9667596707
</li>
9667696708
</ol>
9667796709

96678-
<p class="note"><span>Wait for a timeout</span> is meant to be used by other specifications that
96679-
want to wait on developer-supplied timeouts, in a similar manner to <code
96680-
data-x="dom-setTimeout">setTimeout()</code>. (Note, however, it does not have the nesting and
96681-
clamping behavior of <code data-x="dom-setTimeout">setTimeout()</code>.) Such specifications can
96682-
choose an <var>orderingIdentifier</var> to ensure ordering within their specification's timeouts,
96683-
while not constraining ordering with respect to other specification's timeouts.</p>
96684-
96685-
</div>
96686-
96687-
<div class="example">
96688-
<p>To run tasks of several milliseconds back to back without any delay, while still yielding back
96689-
to the browser to avoid starving the user interface (and to avoid the browser killing the script
96690-
for hogging the CPU), simply queue the next timer before performing work:</p>
96691-
96692-
<pre><code class="js">function doExpensiveWork() {
96693-
var done = false;
96694-
// ...
96695-
// this part of the function takes up to five milliseconds
96696-
// set done to true if we're done
96697-
// ...
96698-
return done;
96699-
}
96700-
96701-
function rescheduleWork() {
96702-
var id = setTimeout(rescheduleWork, 0); // preschedule next iteration
96703-
if (doExpensiveWork())
96704-
clearTimeout(id); // clear the timeout if we don't need it
96705-
}
96710+
<p class="note"><span>Run steps after a timeout</span> is meant to be used by other
96711+
specifications that want to execute developer-supplied code after a developer-supplied timeout,
96712+
in a similar manner to <code data-x="dom-setTimeout">setTimeout()</code>. (Note, however, it does
96713+
not have the nesting and clamping behavior of <code data-x="dom-setTimeout">setTimeout()</code>.)
96714+
Such specifications can choose an <var>orderingIdentifier</var> to ensure ordering within their
96715+
specification's timeouts, while not constraining ordering with respect to other specification's
96716+
timeouts.</p>
9670696717

96707-
function scheduleWork() {
96708-
setTimeout(rescheduleWork, 0);
96709-
}
96710-
96711-
scheduleWork(); // queues a task to do lots of work</code></pre>
9671296718
</div>
9671396719

9671496720

0 commit comments

Comments
 (0)