Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 435619 of Window.setImmediate()

  • Revision slug: Web/API/window.setImmediate
  • Revision title: window.setImmediate
  • Revision id: 435619
  • Created:
  • Creator: paul.irish
  • Is current revision? No
  • Comment updates and pointer to actual polyfill implementation.

Revision Content

This method is used to break-up long running operations and run a callback function immediately after the browser has completed other operations such as events and display updates.

Note: This method is not expected to become standard, and is only implemented by recent builds of Internet Explorer. It meets resistance both from Gecko (Firefox) and Webkit (Google/Apple)

Syntax

var immediateID = setImmediate(func, [param1, param2, ...]);
var immediateID = setImmediate(func);
  • where immediateID is the ID of the immediate which can be used later with {{ domxref("window.clearImmediate") }}.
  • func is the function you wish to call.

All parameters will be passed directly to your function.

Notes

The {{ domxref("window.clearImmediate") }} method can be used to clear the immediate actions, just like {{ domxref("window.clearTimeout") }} for {{ domxref("window.setTimeout") }}.

This method can be used instead of the setTimeout(fn, 0) method to execute heavy operations

The feature can be emulated in a few different ways:

  • {{ domxref("window.postMessage") }} can be used to trigger an immediate but yielding callback. Do note that Internet Explorer 8 includes a synchronous version of postMessage, which means it cannot be used as a fallback.
  • MessageChannel can be used reliably inside of Web Workers whereas the semantics of postMessage mean it cannot be used there.
  • setTimeout(fn, 0) can  potentially be used, however as it is clamped to 4ms per the HTML spec, it does not make for a suitable polyfill for the natural immediacy of setImmediate.

All of these techniques are incorporated into a robust setImmediate polyfill.

Browser compatibility

{{ CompatibilityTable }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatNo }} {{ CompatNo }} 10 {{ CompatNo }} {{ CompatNo }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}

See also

{{ domxref("window.clearImmediate") }}

{{ spec("https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html", "Specification: Efficient Script Yielding") }}

Microsoft setImmediate API Demo

{{ languages( { "zh-cn": "zh-cn/DOM/window.setImmediate" } ) }}

Revision Source

<p>This method is used to break-up long running operations and run a callback function immediately after the browser has completed other operations such as events and display updates.</p>
<div class="note">
  <strong>Note:</strong> This method is not expected to become standard, and is only implemented by recent builds of Internet Explorer. It meets resistance both from <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=686201" title="https://bugzilla.mozilla.org/show_bug.cgi?id=686201">Gecko</a> (Firefox) and <a href="https://code.google.com/p/chromium/issues/detail?id=146172" title="https://code.google.com/p/chromium/issues/detail?id=146172">Webkit</a> (Google/Apple)</div>
<h2 id="Syntax">Syntax</h2>
<pre>
var immediateID = setImmediate(func, [param1, param2, ...]);
var immediateID = setImmediate(func);
</pre>
<ul>
  <li>where <code>immediateID</code> is the ID of the immediate which can be used later with {{ domxref("window.clearImmediate") }}.</li>
  <li><code>func</code> is the function you wish to call.</li>
</ul>
<p>All parameters will be passed directly to your function.</p>
<h2 id="Notes">Notes</h2>
<p>The {{ domxref("window.clearImmediate") }} method can be used to clear the immediate actions, just like {{ domxref("window.clearTimeout") }} for {{ domxref("window.setTimeout") }}.</p>
<p>This method can be used instead of the <code>setTimeout(fn, 0)</code> method to execute <a href="https://www.nczonline.net/blog/2009/08/11/timed-array-processing-in-javascript/" title="https://www.nczonline.net/blog/2009/08/11/timed-array-processing-in-javascript/">heavy operations</a>.&nbsp;</p>
<p>The feature can be emulated in a few different ways:</p>
<ul>
  <li><span style="line-height: 22px;">{{ domxref("window.postMessage") }}&nbsp;</span>can be used to trigger an immediate but yielding callback. Do note that Internet Explorer 8 includes a synchronous version of postMessage, which means it cannot be used as a fallback.</li>
  <li><a href="https://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#channel-messaging" title="https://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#channel-messaging">MessageChannel</a>&nbsp;can be used reliably inside of Web Workers whereas the semantics of postMessage mean it cannot be used there.</li>
  <li><code>setTimeout(fn, 0)</code>
    <i>
      can&nbsp;</i>
    potentially be used, however as it is clamped to 4ms <a href="https://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout" title="https://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout">per the HTML spec</a>, it does not make for a suitable polyfill for the natural immediacy of setImmediate.</li>
</ul>
<p>All of these techniques are incorporated into a <a href="https://github.com/NobleJS/setImmediate" title="https://github.com/NobleJS/setImmediate">robust setImmediate polyfill</a>.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
        <td>10</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
        <td>{{ CompatNo }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<p>{{ domxref("window.clearImmediate") }}</p>
<p>{{ spec("https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html", "Specification: Efficient Script Yielding") }}</p>
<p><a class="external" href="https://ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html">Microsoft setImmediate API Demo</a></p>
<p>{{ languages( { "zh-cn": "zh-cn/DOM/window.setImmediate" } ) }}</p>
Revert to this revision