YouTip LogoYouTip

Misc When

# jQuery.when() Method [![Image 4: jQuery Misc Methods](#)jQuery Misc Methods](#) ## Example One argument passed to $.when() is accepted, 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. **Note:** 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 to the Promise object, for example, deferred.then. When the deferred object has been resolved or rejected (typically by the code that originally created the deferred object), the corresponding callback function will be called. * * * ## Syntax _$_.when( deferreds ) | Parameter | Description | | :--- | :--- | | _deferreds_ | Deferred type. One or more deferred objects, or plain JavaScript objects. | * * * ![Image 5: Example](#) ## More Examples If you pass no arguments, jQuery.when() will return a resolved promise object. ## Example Pass no arguments, execute the callback function $(function () { $.when().then(function( x ) { alert( "I fired immediately" ); }); }) [Try it Β»](#) When multiple deferred objects are passed to jQuery.when(), the method returns a new "master" deferred object. The method will only resolve its master deferred object when all deferred objects have been resolved. If one of the deferred objects is rejected, the method will reject its master deferred object. When the master object is resolved, the doneCallbacks will be executed. ## Example Pass multiple deferred objects $(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" ); }) [Try it Β»](#) If no value is passed to the deferred object's resolved event, the corresponding doneCallback parameter will be undefined. If a single value is passed to the deferred object's resolved event, the corresponding parameter will retain that value. If multiple values are passed to the deferred object's resolved event, the corresponding parameter will be an array of those values. ## Example Pass multiple deferred objects of different types $(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 ); }) [Try it Β»](#) In the case of multiple deferreds, if one of the deferred objects is rejected, jQuery.when() immediately triggers the failCallbacks callback function of the "master" deferred object. ## Example Call the failCallbacks callback function when one of multiple deferred objects is rejected $(function () { $.when($.ajax("/page1.php"), $.ajax("/page2.php")).then(function(data, textStatus, jqXHR){ alert(jqXHR.status); }, function(obj){ alert(obj.statusText); }); }) [Try it Β»](#) * * jQuery Misc Methods](#)
← Misc Callbacks AddCss3 Pr Overflow Style β†’