jQuery.when() Method
Example
One argument passed to $.when() is resolved, executing the callback function
$
(
function
(
)
{
$.
when
(
{
testing
:
123
}
)
.
done
(
function
(
x
)
{
alert
(
x
.
testing
)
;
}
/*
alerts "123"
*/
)
;
}
)
Try it »
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 the Deferred methods) is returned. You can continue to bind other methods of the Promise object, such as deferred.then. When the Deferred object has been resolved or rejected (usually performed by the original code that created the Deferred object), the corresponding callback function is called.
Syntax
$
.when( deferreds )
| Parameters | Description |
|---|---|
| deferreds | Deferred type: One or more Deferred objects, or plain JavaScript objects |
More examples
If you do not pass any arguments, jQuery.when() will return a Promise object in the resolved state.
Example
Pass no arguments and execute the callback function
[mycode3 type="javascript"] $(function () { $.when().then(function( x ) { alert( "I fired immediately" ); }); }) [/mycode3]
Try it »
When multiple Deferred objects are passed to jQuery.when(), the method returns a new "master" Deferred object. It only resolves its master Deferred object when all the Deferred objects have been resolved. If any one of the Deferred objects is rejected, the method rejects its master Deferred object. When the master object is resolved, the doneCallbacks (resolve callbacks) will be executed.
Example
Pass multiple Deferred objects
[mycode3 type="javascript"] $(function () { var d1 = $.Deferred(); var d2 = $.Deferred(); $.when( d1, d2 ).done(function ( v1, v2 ) { alert( v1 ); // "Fish" alert( v2 ); // "Pizza" }); d1.resolve( "Fish" ); d2.resolve( "Pizza" ); }) [/mycode3]
Try it »
If no value is passed to a Deferred object's resolved event, the corresponding doneCallback argument will be undefined. If a single value is passed to the Deferred object's resolved event, the corresponding argument will hold that value. When multiple values are passed to the Deferred object's resolved event, the corresponding argument will be an array of those values.
Example
Pass multiple Deferred objects of different types
[mycode3 type="javascript"] $(function () { var d1 = $.Deferred(); var d2 = $.Deferred(); var d3 = $.Deferred(); $.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) { alert( v1 ); // v1 is undefined alert( v2 ); // v2 is "abc" alert( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ] }); d1.resolve(); d2.resolve( "abc" ); d3.resolve( 1, 2, 3, 4, 5 ); }) [/mycode3]
Try it »
In the case of multiple Deferreds, if one of the Deferred objects is rejected, jQuery.when() immediately triggers a call to the "master" Deferred object's failCallbacks.
Example
Call the failCallbacks when one of the multiple Deferred objects is rejected
[mycode3 type="javascript"] $(function () { $.when($.ajax("/page1.php"), $.ajax("/page2.php")).then(function(data, textStatus, jqXHR){ alert(jqXHR.status); }, function(obj){ alert(obj.statusText); }); }) [/mycode3]
Try it »
jQuery Miscellaneous Methods