Copyright © 2015 W3C? (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.
This specification defines an interface for web applications to flush the browser event queue and receive an immediate callback.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document is no longer under development. Do Not Reference.
This section is non-normative.
The Efficient Script Yielding specification defines a means for site developers
to yield control flow to the user agent before running script. Web developers
may want to use this interface to both allow user agent events and display
updates to happen before continuing script execution and to avoid long running
script dialogs in certain user agents. Many web pages use the
setTimeout
method with a specified period of zero to attempt to do the same thing. However,
setTimeout
enforces a minimum delay, even with a specified period of zero, that isn't
uniformly implemented across user agents. Removing this minimum delay from
setTimeout
runs the risk of causing existing webpages, that have come to rely on the
minimum delay, to break by going into a seemingly hung state while also significantly increasing the power consumption of the browser.
This specification defines a new method,
setImmediate
, which will run a callback function immediately after
the user agent events and display updates have occurred. This interface will not
enforce a minimum delay and will attempt to run the callback as soon as it can.
For example, the following Javascript shows a theoretical web based email client attempting to do a spellcheck operation after the user agent events and display updates have been flushed, to ensure that the site remains responsive:
<!DOCTYPE html> <html> <head> <script> function emailClient() { // Do email work // When UI is free, check for spelling setTimeout(checkSpelling, 0); } function checkSpelling() { // Scan email for spelling errors } </script> </head> <body onload="emailClient()"> </body> </html>
The script callback will always be subject to a minimum delay that may not be uniformly defined across user agents. This results in script that is not running in the most performant manner.
Using the setImmediate
method, the page will be able to efficiently yield control flow without minimum
delays.
For example, the following Javascript shows a theoretical web based email client
attempting to do a spellcheck operation with the
setImmediate
method to efficiently yield control flow without minimum
delays:
<!DOCTYPE html> <html> <head> <script> function emailClient() { // Do email work // When UI is free, check for spelling setImmediate(checkSpelling); } function checkSpelling() { // Scan email for spelling errors } </script> </head> <body onload="emailClient()"> </body> </html>
All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.
The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. For readability, these words do not appear in all uppercase letters in this specification.
Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should", "may", etc) used in introducing the algorithm.
Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements are to be interpreted as requirements on user agents.
Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)
The construction "a Foo
object", where Foo
is actually an interface, is sometimes used instead of the more
accurate "an object implementing the interface Foo
".
The term DOM is used to refer to the API set made available to scripts in Web
applications, and does not necessarily imply the existence of an actual
Document
object or of any other Node
objects as defined in
the DOM Core specifications.
A DOM attribute is said to be getting when its value is being retrieved (such as by author script), and is said to be setting when a new value is assigned to it.
The term "JavaScript" is used to refer to ECMA-262, rather than the official term ECMAScript, since the term JavaScript is more widely known.
This section is non-normative
This specification introduces an interface that allows web applications to efficiently yield script control without subjecting running the callback to minimum delays.
WindowTimersExtension
interface[NoInterfaceObject] interface WindowTimersExtension { long setImmediate(in any handler, in optional any... args); void clearImmediate(in long handle); }; Window implements WindowTimersExtension;
setImmediate
( handler [, arguments
] )Schedules to run handler immediately after user agent events have been flushed. Any arguments are passed straight through to the handler.
clearImmediate
( handle )Cancels the immediate set with setImmediate() identified by handle.
This API does not guarantee that the callback will be run immediately. Delays due to CPU load, other tasks, etc, may occur.
Each object that implements the
WindowTimersExtension
interface has a list of active immediates.
Each entry in these lists is identified by a number, which must be unique within
its list for the lifetime of the object that implements the
WindowTimersExtention
interface.
setImmediate
method must run the
following steps:
Let handle be a user-agent-defined integer that is greater than zero that will identify the immediate to be set by this call.
Add an entry to the list of active immediates for handle.
Get the handle set by this setImmediate() call from the list of active immediates, and let task be the result.
Continue running this algorithm asynchronously.
Wait until the
Document
is fully active.
Wait until any invocations of this algorithm started before this one have completed.
When the above method is invoked and tries to get immediate in list list, it must run the following steps:
If the first argument to the invoked method is an object that has an internal [[Call]] method, then return a task that checks if the entry for handle in list has been cleared, and if it has not, calls the aforementioned [[Call]] method with as its arguments the second and subsequent arguments to the invoked method (if any) and abort these steps. [ECMA262]
Otherwise, continue with the remaining steps.
Apply the ToString() abstract operation to the first argument to the method, and let script source be the result. [ECMA262]
Let script language be JavaScript.
Return a task that checks if the entry for handle in list has been cleared, and if it has not, creates a script using script source as the script source, scripting language as the scripting language.