Explain the use of deferred objects in jquery (i) _jquery

Source: Internet
Author: User

The deferred object is the implementation of jquery's promises interface. It is a common interface for asynchronous operations, and can be seen as a waiting task for developers to set up through a number of interfaces. In fact, it acts as a proxy, wrapping those asynchronous operations into objects with some unifying features, typically AJAX operations, Web animations, web worker, and so on.

All of the Ajax operation functions of jquery, the default return is a deferred object.

In the jquery1.5 version, a deferred object, a deferred object, is added to handle the callback function that occurs at a future point in time. Also, the Ajax method is rewritten, and the Ajax method now returns a deferred object.

Let's take a look at the usage of the deferred object.

Chain Callback for 1.ajax

The Ajax method returns a deferred object that can be written directly using the
$.ajax (' Test.json '). Done (function (RESP) {
//done equivalent to success callback, Where the default parameter is the parameter
alert (' success ') of the success callback;
Fail (function () {
//fail is equivalent to error callback
alert (' Error ');

You can also write multiple callbacks at the same time, which are sequentially executed in sequence

$.ajax (' Test.json '). Done (the function (RESP) {
//done corresponds to the success callback, where the default parameter is the parameter
alert (' success ') of the success callback;
}). Done (function () {
//do something ...
}). Done (function () {
//do something ...
}); 

Deferred object also has a then method, in fact it is an integrated done and fail method, it accepts one or two parameters, if there are two parameters, then the first is the done method of the callback function, the second is the fail method of the callback function. If there is only one argument, it is the callback function of the Done method.

var success = function () {
alert (' success ');
var error = function () {
alert (' Error ');
Two parameters
$.ajax (' Test.json '). Then (success, error);
A parameter

jquery also provides a $.when (Deferreds) method to execute a callback function for one or more deferred objects, and when its arguments are deferred, it executes the corresponding callback function after the asynchronous execution of all deferred objects.

$.when ($.ajax (' Test.json '), $.ajax (' Demo.json '). Done (function () {
alert (' success '); 
}). Fail (function () {
alert (' Error ');

It is well understood that the callback in the Done method is executed only if all asynchrony succeeds, otherwise the callback in the Fail method is executed, which is equally well understood. The default number of parameters for a callback function in the Done method is the same as when method parameters.

The callback in the done method is executed immediately if the When method passes in only the normal object, not the deferred object, and the default parameter of the callback function is the object itself that is passed in the When method.

$.when when the argument to the When method is just a normal object
({test: ' Test '} '). Done (function (resp) {
console.log (resp.test);//' Test ' 
} . Fail (function () {
//) callback in fail is not invoked because the object passed in is not a deferred object. 

When you need two or more asynchronous end to call the callback function, while these asynchronous Ajax may also need to modify the transmission mode type or data transfer, the code is very messy and very readable.

So you can encapsulate Ajax and improve the readability of your code.

var ajax = function (URL, type, param) {return
$.ajax ({
url:url,
type:type,
data:param | | {} 
}); 
};
Ajax (' Test.json '). Done (function (RESP) {
alert (' success ');
}). Fail (function () {
alert (' Error ');
});

The above is a small set to introduce jquery in the use of deferred objects (i) of the relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.