Latest web development tutorials

jQuery.when () method

jQuery Misc Methods jQuery Misc Methods

Examples

A parameter passed to the $ .when () is accepted, the implementation of the callback function

$ (Function () {$. When ( { testing: 123 } ). Done ( function (x) { alert (x testing.);} / * Alerts "123" * / );})

try it"

Definition and Usage

$ .when () Function provides a way to execute one or more objects of the callback function.

Tip: If you pass an object to a delay jQuery.when, then it will return Promise objects (a subset of the delay method). Other methods can continue to bind the Promise object, for example, defered.then. When the delay object has been accepted (resolved) or rejected (rejected) (usually created by the delay in the initial object code execution), it will call the corresponding callback function.


grammar

$.when( deferreds )

参数 描述
deferreds Deferred类型 一个或多个延迟对象,或者普通的JavaScript对象


Examples

More examples


If you do not pass any parameters, jQuery.when () returns a resolved (accepted) promise object's state.

Examples

Do not pass any parameters, perform the callback function

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

try it"

A plurality of delay in the case of the object passed to jQuery.when (), the method returns a new "host" lingering objects when all the objects are delayed acceptance (resolve), the method will not accept its host lingering objects . When there is a delay in which the object is denied (rejected), this method will reject its host lingering objects. When a host object is accepted, doneCallbacks (accept callback) will be executed.

Examples

A plurality of delay incoming 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 the delay in receiving the object (resolved) event, then the corresponding doneCallback argument will be undefined. If you pass an object to the delay in receiving (resolved) event as a single value, the corresponding parameters will retain that value. In passing to resolve lingering objects (resolved) into a plurality of event value, the corresponding argument will be an array of these values.

Examples

Incoming number of different types of lingering objects

[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 a plurality of delay, if the delay is one of the object is denied (rejected), jQuery.when () call immediately trigger "host" lingering objects failCallbacks callback function.

Examples

Callback function call failCallbacks one of a plurality of delay object denied

[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 Misc Methods jQuery Misc Methods