Requests that a function be called when current or pending tasks are complete, such as events or screen updates.
Syntaxvar retVal = window.setImmediate(handler, arguments);
- handler [in]
The function to be called.
- arguments [in, optional]
Arguments to be passed to the function.
A handle to the request.
The setImmediate method schedules the function specified in the handler parameter to run after the current script block completes. If additional actions are pending when the current script block completes, they are processed before the handler function is called. This effectively creates a yield between the current script block and the handler function.
If you break extended operations into separate functions, you can use setImmediate to call each function in sequence. When you do this, setImmediate allows additional tasks to complete before calling each function in the sequence. In turn, this enables the application to respond to user input and to handle additional tasks in a predictable and responsive fashion.
For more information, see the setImmediate TestDrive demo.
The setTimeout method is restricted to 250 requests per second on most systems. This means that
setTimeout(0, handler) waits roughly 4ms before executing, even if no additional actions are pending. In contrast, setImmediate yields between each request, no matter how many requests are waiting to processed. If no additional actions are pending, setImmediate calls the handler function immediately.