jQuery.when()Method
Example
A parameter passed to $.when() is resolved, and the callback function is executed.
Try it Yourself »
Definition and Usage
The $.when() function provides a way to execute callback functions for one or more objects.
Tip:If a deferred object is passed to jQuery.when, its Promise object (a subset of deferred methods) is returned. You can continue to bind other methods of the Promise object, such as defered.then. When the deferred object has been resolved or rejected (usually performed by the initial code that created the deferred object), the corresponding callback functions will be called.
Syntax
| Parameter | Description |
|---|---|
| deferreds | Deferred type: One or more deferred objects, or ordinary JavaScript objects. |
More Examples
If you do not pass any parameters, jQuery.when() will return a promise object in the resolved state.
Example
Execute the callback function without passing any parameters
Try it Yourself »
In the case where multiple deferred objects are passed to jQuery.when(), this method returns a new "host" deferred object. It resolves its host deferred object only when all deferred objects are resolved. When any one of the deferred objects is rejected, it rejects its host deferred object. When the host object is resolved, the doneCallbacks (resolution callbacks) will be executed.
Example
Pass in multiple deferred objects
Try it Yourself »
If no value is passed to the resolved event of a deferred object, the corresponding doneCallback parameter will be undefined. If a single value is passed to the resolved event of a deferred object, the corresponding parameter will retain that value. In the case where multiple values are passed to the resolved event of a deferred object, the corresponding parameter will be an array of these values.
Example
Pass in multiple deferred objects of different types
Try it Yourself »
In the case of multiple deferred objects, if one of the deferred objects is rejected, jQuery.when() immediately triggers the failCallbacks callback function of the "host" deferred object.
Example
Call the failCallbacks callback function when one of multiple deferred objects is rejected
Try it Yourself »
Other Extensions
jQuery Miscellaneous Methods