$.when's description
Description: Provides a way to execute a callback function for one or more objects that returns a delay (Deferred) object for those objects.
Description (combined with examples and source code):
If you do not pass any arguments , the jQuery.when()
promise object that returns a resolved (resolved) state is returned.
If a parameter is passed to Jquery.when () and it is not a deferred or Promise object, it is treated as a deferred object that is resolved (resolved) and bound to any of the above Donecallbacks will be executed immediately. The original parameters are passed into the donecallbacks. In this case, any failcallbacks that are set will never be executed because the deferred object will never be rejected (rejected). For example:
$.when ({testing:123 }). Done ( function/*/);
The above two cases, the source code
// If there is no pass parameter or only one parameter and is not a delay object, the main delay object can be resolved directly if ( ! remaining) { Deferred.resolvewith (resolvecontexts, resolvevalues); }
If jQuery.when()
a single Deferred object is passed in, its Promise object (a subset of the deferred method) is returned. You can continue to bind other methods of the Promise object, for example, Defered.then. The appropriate callback function is called when the deferred object has been resolved (resolved) or Rejected (rejected) (usually performed by the initial code that created the deferred object). For example, the jQuery.ajax()
returned JQXHR object is a single deferred object and can be used as follows:
$.when ($.ajax ("Test.aspx")). Then (function(data, Textstatus, JQXHR) { // Alerts );
Source
return deferred.promise ();
Multiple lingering objects passed to jquery.when ()
, the method is based on a new "host" Deferred (deferred) object, Tracks all passed Deferreds aggregation states, returning a Promise object. When all lingering objects are resolved (resolve), the "host" Deferred (delay) object will resolve (resolved) the method, the source
// Otherwise, the member delay (Deferred) object is resolved before entering the branch to Judge // the number of delay (Deferred) objects that need to wait to be resolved is reduced by one, // determine if there are any delay (Deferred) objects that need to wait for resolution, and if so, the judgment fails to return // if not then go into the branching sub-code, execute the main delay (Deferred) object to resolve the Deferred.resolvewith (... )Elseif (! (--remaining)) { // At the end of this step, the values have become the parameter sequences of all member delay (Deferred) objects resolve deferred.resolvewith (contexts, values);}
Or when one of the deferred objects is rejected (rejected), the "host" Deferred (delay) object reject (Rejects) the method, the source
resolvevalues[i].promise (). Done (Updatefunc (i, resolvecontexts, resolvevalues)) // if a member delay (Deferred) object is rejected, the entire main delay (Deferred) object is rejected directly . Fail (Deferred.reject). Progress (Updatefunc (I, progresscontexts, progressvalues));
if the host Deferred (deferred) object is a (resolved) resolution state, the Donecallbacks (resolution callback) of the host Deferred (deferred) object is executed. The arguments passed to donecallbacks provide this resolution (resolved) value to each corresponding Deferreds object, and match the jQuery.when()
order in which Deferreds is passed. For example:
var d1 = $. Deferred (); var d2 = $. Deferred (); $.when (D1, D2). Done (function (v1, v2) { // "Fish" // "Pizza" "Fish""Pizza");
In the case of multiple latencies, if the Deferreds delay object is rejected (rejected), Jquery.when () triggers an immediate call to the failcallbacks of the "host" Deferred (delay) object. Please note that at this point in time, some lingering objects can still be unresolved (unresolved). The arguments passed to failcallbacks match the order in which the failcallbacks of the deferred (delay) object is rejected rejected. So, in this case, if you need to do some extra processing, for example, to cancel all outstanding Ajax requests, you can keep a reference to the JQXHR object in the closure and check or cancel them in Failcallback.
Complete source code is attached
//Deferred HelperWhen:function(Subordinate/*,..., Subordinaten*/ ) { vari = 0, Resolvevalues=Core_slice.call (arguments), length= Resolvevalues.length,//Number of parameters //statistics are not resolved members, //If the number of arguments is not 1, then remaining is the number of arguments, //if the number of arguments is 1 and the argument is a Deferred (deferred) object, remaining is the parameter number 1; otherwise 0Remaining = length!== 1 | | (Subordinate && Jquery.isfunction (subordinate.promise))? length:0, //initializes the main delay object, if the parameter itself is a delay object then use the parameter, otherwise new.Deferred = remaining = = = 1?subordinate:jQuery.Deferred (),//The update function handles the values of resolve and progressUpdatefunc =function(i, contexts, values) {return function(value) {contexts[i]= This; values[i]= arguments.length > 1?core_slice.call (arguments): value; //when a member delay (Deferred) object generates a progress notification, the main delay (Deferred) object calls the in-process callback
Later Generationscode. Progress (Updatefunc (I, progresscontexts, progressvalues), the third parameter when adding a function to the progress callback list is progressvalues if(Values = = =progressvalues) {
//invoke in-progress callback (Progresscallbacks)Deferred.notifywith (contexts, values); //Otherwise, the member delay (Deferred) object is resolved before entering the branch to Judge //the number of delay (Deferred) objects that need to wait to be resolved is reduced by one, //determine if there are any delay (Deferred) objects that need to wait for resolution, and if so, the judgment fails to return //if not then go into the branching sub-code, execute the main delay (Deferred) object to resolve the Deferred.resolvewith (... )}Else if( ! ( --remaining)) { //At the end of this step, the values have become the parameter sequences for all member delay (Deferred) object ResolveDeferred.resolvewith (contexts, values); } }; }, Progressvalues, progresscontexts, resolvecontexts; //for a parameter that has no parameters or a unique non-deferred object, the resolved is considered directly, that is, the processing of the later if (!remaining) branch //for a parameter is a delay object that directly returns the Deferred.promise () of the delay object. //for more than one parameter to enter the branch, add listener listening member is resolved if(Length > 1) {progressvalues=NewArray (length); Progresscontexts=NewArray (length); Resolvecontexts=NewArray (length); for(; i < length; i++ ) { //A listener is required to traverse parameter members only if the member is a delay (Deferred) object if(resolvevalues[i] &&jquery.isfunction (resolvevalues[i].promise)) {resolvevalues[i].promise (). Done (Updatefunc (i, resolvecontexts, resolvevalues)) //if a member delay (Deferred) object is rejected, the entire main delay (Deferred) object is rejected directly. Fail (Deferred.reject). Progress (Updatefunc (I, progresscontexts, progressvalues)) ; //Non-delay (Deferred) objects are directly considered resolved, and the number of delay (Deferred) objects that need to wait for resolution is reduced by one}Else { --remaining; } } } //If there is no pass parameter or only one parameter and is not a delay object, the main delay object can be resolved directly if( !remaining) {Deferred.resolvewith (resolvecontexts, resolvevalues); } returndeferred.promise ();}
jQuery-1.9.1 Source Analysis Series (vi) Delay object Continuation--auxiliary function Jquery.when