JQuery Source: Deferred Object supplement.

Source: Internet
Author: User

Deferred Helper (3132)
When is a helper method for delaying object deferred.

    var DFD = $. Deferred ();        Create a deferred object    DFD. Done ();    Dfd.fail ();    Used:    $.when (). done ();    $.when (). fail ();

The return value of when, is a lingering object.
Source: Return Deferred.promise ();

As an example: when you can wait for multiple lingering objects to succeed, the trigger succeeds.
Example: (1)

Success must be successful for more than one. function aaa () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' aaa ');d fd.resolve ();}, +); return DFD;} function bbb () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' bbb ');d fd.resolve ();}, +); return DFD;} $.when (AAA (), BBB ()). Done (function () {alert (' Done ');});
The goal is to trigger this success after both AAA and BBB are complete. Only one lingering object succeeds and does not determine success. This is when AAA and BBB are all done. Before the done function is executed.

Example: (2)
Failure triggers, as long as a failure is possible. And when it fails, it triggers the callback function immediately.

function aaa () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' aaa ');d fd.reject ();}, +); return DFD;} function bbb () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' bbb ');d fd.resolve ();}, +); return DFD;} $.when (AAA (), BBB ()). Fail (function () {alert (' fail ');});

The goal is that AAA and BBB have a failure and immediately set out to fail this function.


Here The Argus is the parameter, a number of deferred objects representing the state.
$.when (Argus). Done (function () {
Alert (' Done ');
}). Fail (function () {
Alert (' fail ');
});

Argus[i] corresponds to each lingering object, jquery has a counter. To calculate the state of all Argus.
As long as the condition is met, the corresponding event can be triggered.

For example, pass in three parameters:
$.when (A, B, C). Done (function () {
Alert (' Done ');
}). Fail (function () {
Alert (' fail ');
});

Then a counter is generated in the code. To determine if the three lingering objects have been completed,
If you complete one, then-1, when the counter is 0, the trigger succeeds.
Of course, if a lingering object fails, it triggers the failed callback function directly.

Example (3)

function aaa () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' AAA ');//Dfd.reject ();}, +); return DFD;} function bbb () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' bbb ');d fd.resolve ();}, +); return DFD;} $.when (AAA (), BBB ()). Done (function () {alert (' Done ');}). Fail (function () {alert (' fail ');});

Note: The result of the execution is that the AAA BBB is printed, but the event is not triggered because the state change of the deferred object has not been triggered in the AAA () object.
So, when you can only wait.

Example (4)
function aaa () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' AAA ');//Dfd.reject ();}, +); return DFD;} function bbb () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' bbb ');d fd.resolve ();}, +); return DFD;} $.when (AAA (), BBB ()). Done (function () {alert (' Done ');}). Fail (function () {alert (' fail ');});

The test results are: Print AAA BBB, then eject done.
Why is it? The status is complete, but AAA does not return. result, The Reject in AAA () does not work.
Because the deferred object is passed in in $.when (), but the function does not return a value, there is no delay in the object's incoming,
Therefore, the AAA (), this parameter, there is no way to play a role. Just jump over. Because BBB () is completed, it causes the trigger to succeed.

Example (5)

A: State $.when (). Done (function () {alert (' Done ');}). Fail (function () {alert (' fail ');}); B: State $.when (123, 1234). Done (function () {alert (' Done ');}). Fail (function () {alert (' fail ');});

Both a state and B state are directly triggered successfully.
When the parameter Argus in $.when (Argus) is not a lingering object, it skips directly, which is equivalent to not writing.

Example (6)

function aaa () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' aaa ');d fd.reject ();};//return DFD;} function bbb () {var DFD = $. Deferred ();//Create Delay Object settimeout (function () {console.log (' bbb ');d fd.resolve ();}, +); return DFD;} $.when (AAA (), BBB (), 123). Done (function () {alert (' do '); alert (arguments[2]);}).     Fail (function () {alert (' fail '); alert (arguments[2]);}); Here Arguments[0] is 123, Arguments[1] is 1234.$.when (123, 1234). Done (function () {alert (' Done ');}). Fail (function () {alert (' fail ');});

Example (7)

$.when (AAA (), 123, BBB (). 345). Done (function () {alert (' done '); alert (arguments[1]); alert (arguments[3]);}). Fail (function () {alert (' fail '); alert (arguments[1]); alert (arguments[3]);});

There are 4 parameters passed in, but only 2 are lingering objects, so it is necessary to judge and update the--remaining operation, and it will not remove the deferred object.


Summarize the thought: first of all, to interpret the parameters passed in, there are several lingering objects, if one does not, do it directly. Self-executing done,fail is not performed.
If yes, the test execution, judge whether the counter is 0, and is a successful return (resolve), for 0 to execute the corresponding success function,
If there is a failure (reject), OK, that is the direct call to fail is good.

When source code://Deferred Helperwhen:function (Subordinate/*, ..., Subordinaten */) {var i = 0,//will arguments---"" converted to an array. Resolvevalues = core_slice.call (arguments), length = resolvevalues.length,//The Count of uncompleted subordinates//(1) This is the counter, to determine if there is still an unfinished situation//(2) If the incoming is empty, return 0, if it is a normal delay object, return length. Jquery.isfunction (subordinate.promise), judging whether the incoming is not a lingering object. Remaining = length!== 1 | | (Subordinate && Jquery.isfunction (subordinate.promise))? length:0,//the Master Deferred. If Resolvevalues consist of only a single Deferred, just use that.deferred = remaining = = = 1? Subordinate:jQuery.Deferred (),//Update function for both resolve and progress values//here is the decrement counter, and determine whether to execute. Updatefunc = function (i, contexts, values) {return function (value) {contexts[i] = this;values[I] = Arguments.lengt H > 1? Core_slice.call (arguments): value;if (values = = = Progressvalues) {Deferred.notifywith (contexts, values);} else if ( ! (--remaining)) {//This is reduced to 0, triggering. Deferred.resolvewith (Contexts, values);}};},progressvalues, progresscontexts, resolvecontexts;//add listeners to Deferred subordinates; Treat others as resolvedif (length > 1) {progressvalues = new Array (length);p rogresscontexts = new Array (length); resolvecontexts = new Array (length);//filter non-lingering objects. for (; i < length; i++) {//Determine if it is a lingering object. Resolvevalues This is the array that is returned before the incoming parameter is processed. if (resolvevalues[i] && jquery.isfunction (resolvevalues[i].promise)) {resolvevalues[I].promise (). Done (U Pdatefunc (i, resolvecontexts, resolvevalues))//It is also necessary to determine whether the counter is up to 0. Fail (deferred.reject)//not judged, directly triggered. As long as one is not completed, it is triggered directly: progress (Updatefunc (I, progresscontexts, progressvalues));} If the object is not lingering, then----the counter is reduced. else {--remaining;}}} If we ' re not waiting on anything, resolve the master//here gives an explanation for the status of the parameter not passed in. is to execute directly. if (!remaining) {Deferred.resolvewith (resolvecontexts, resolvevalues);} return Deferred.promise ();}

JQuery Source: Deferred Object supplement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.