The deferred object, introduced in 1.5 in jquery, is a jQuery.Deferred()
tool object that creates a chained invocation by invoking a method. It can register multiple callbacks to the callback queue, call the callback queue, and prepare to replace any synchronous or asynchronous function with a successful or failed state.
Deferred.alway ()
When the deferred (deferred) object is resolved or rejected, the add handler is called.
Deferred.always (Alwayscallbacks [, Alwayscallbacks])
Parameter alwayscallbacks type: A function or function array
Example:
1 function () {2 console.log ("$.get completed with success or error callback arguments"); 3 } ); 4 Object {readystate:1}5678 with success or error callback arguments
Deferred.done ()
When the deferred object is closed, the handler is added and the parameter is the same as Deferred.alway ()
Example:
1 functionB () {2Console.log ("Test");3 }4 5 6 varDtd=$. Deferred (); Creating lingering Objects7 Dtd.resolve (); Changing lingering objects, delaying object resolution8Dtd.done (b). Done (function(){9Console.log ("B is done");Ten }); One A Test - -B is done
Deferred.fail ()
Called when a deferred object is rejected
1 var dtd = $. Deferred (); 2 Dtd.reject (); 3 dtd.fail (function() {Console.log ("fail")}); 4 5 Fail
Deferred.resolve ()
Set the lingering object to resolve and execute the callback function;
Deferred.reject ()
Denies lingering objects and executes callback functions;
Deferred.state ()
Determines the current state of a lingering object. This method does not accept parameters and returns only one string for the following three:
Resolved: Indicates that the lingering object is in the resolution state;
Rejected: Indicates that the deferred object is a deny state;
Pending: Indicates that lingering objects have not been resolved;
Deferrred.then ()
The add handler is called when the deferred (deferred) object is resolved, rejected, or still in progress.
Example:
1 functionA1 () {2 varDTD = $. Deferred (); 3Console.log ("AA");4 dtd.resolve ();5 6 returnDTD;7 }8 9 varan=A1 ();Ten OneAn.then (function() { AConsole.log ("an"); - }) - the AA - -An
Jquery Lingering objects