jQuery.when()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Example

A parameter passed to $.when() is resolved, and the callback function is executed.

$(function () { $.when( { testing: 123 } ).done( function(x) { alert(x.testing); } /* alerts "123" */ ); })

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

$.when( deferreds )

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

[mycode3 type="javascript"] $(function () { $.when().then(function( x ) { alert( "I fired immediately" ); }); }) [/mycode3]

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

[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 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

[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 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

[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 Yourself »


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions