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
<p>If {{domxref("Window.requestIdleCallback", "window.requestIdleCallback")}} is undefined, we create it here. The function begins by recording the time at which our implementation was called. We'll be using that to compute the value returned by our shim for {{domxref("IdleDeadline.timeRemaining()", "timeRemaining()")}}.</p>
57
57
58
-
<p>Then we call {{domxref("WindowOrWorkerGlobalScope/setTimeout", "setTimeout()")}}, passing into it a function which runs the callback passed into our implementation of <code>requestIdleCallback()</code>. The callback is passed an object which conforms to {{domxref("IdleDeadline")}}, with {{domxref("IdleDeadline.didTimeout", "didTimeout")}} set to <code>false</code> and a{{domxref("IdleDeadline.timeRemaining", "timeRemaining()")}} method which is implemented to give the callback 50 milliseconds of time to begin with. Each time <code>timeRemaining()</code> is called, it subtracts the elapsed time from the original 50 milliseconds to determine the amount of time left.</p>
58
+
<p>Then we call {{domxref("WindowOrWorkerGlobalScope/setTimeout", "setTimeout()")}}, passing into it a function which runs the callback passed into our implementation of <code>requestIdleCallback()</code>. The callback is passed an object which conforms to {{domxref("IdleDeadline")}}, with {{domxref("IdleDeadline.didTimeout", "didTimeout")}} set to <code>false</code> and a{{domxref("IdleDeadline.timeRemaining", "timeRemaining()")}} method which is implemented to give the callback 50 milliseconds of time to begin with. Each time <code>timeRemaining()</code> is called, it subtracts the elapsed time from the original 50 milliseconds to determine the amount of time left.</p>
59
59
60
60
<p>As a result, while our shim doesn't constrain itself to the amount of idle time left in the current event loop pass like the true <code>requestIdleCallback()</code>, it does at least limit the callback to no more than 50 milliseconds of run time per pass.</p>
61
61
@@ -65,7 +65,7 @@ <h3 id="Falling_back_to_setTimeout">Falling back to setTimeout</h3>
65
65
clearTimeout(id);
66
66
}</pre>
67
67
68
-
<p>If <code>cancelIdleCallback()</code> isn't defined, this creates one whichpasses the specified callback ID through to {{domxref("WindowOrWorkerGlobalScope/clearTimeout", "clearTimeout()")}}.</p>
68
+
<p>If <code>cancelIdleCallback()</code> isn't defined, this creates one whichpasses the specified callback ID through to {{domxref("WindowOrWorkerGlobalScope/clearTimeout", "clearTimeout()")}}.</p>
69
69
70
70
<p>Now your code will work even on browsers that don't support the Background Tasks API, albeit not as efficiently.</p>
<p>Finally, we set up a couple of variables for other items:</p>
238
238
239
239
<ul>
240
-
<li><code>logFragment</code> will be used to store a {{domxref("DocumentFragment")}} that's generated by our logging functions tocreate content to append to the log when the next animation frame is rendered.</li>
240
+
<li><code>logFragment</code> will be used to store a {{domxref("DocumentFragment")}} that's generated by our logging functions tocreate content to append to the log when the next animation frame is rendered.</li>
241
241
<li><code>statusRefreshScheduled</code> is used to track whether or not we've already scheduled an update of the status display box for the upcoming frame, so that we only do it once per frame</li>
0 commit comments