jQuery中的deferred使用方法
deferred簡(jiǎn)介
deferred對(duì)象是jQuery的回調(diào)函數(shù)解決方案,jQuery之前的版本中異步回調(diào)這塊做的不是很好,所以后期補(bǔ)上了該解決方案。

普遍的ajax操作方式
我們先來回顧一下jQuery中普通的ajax操作
$.ajax({
url: 'test.html',
success: function (res) {
console.log('數(shù)據(jù)讀取成功');
},
error: function () {
console.log('數(shù)據(jù)讀取失敗');
}
});
1.5版本后的新寫法如下:
$.ajax('test.html').done(function (res) {
console.log('數(shù)據(jù)讀取成功');
}).fail(function () {
console.log('數(shù)據(jù)讀取失敗');
});
新版本的寫法相比老版本有一個(gè)優(yōu)勢(shì),就是可以自由添加多個(gè)回調(diào)函數(shù)并且按照添加順序執(zhí)行:
$.ajax('test.html').done(function (res) {
console.log('數(shù)據(jù)讀取成功');
}).fail(function () {
console.log('數(shù)據(jù)讀取失敗');
}).done(function (res) {
console.log('這是第二個(gè)成功的回調(diào)函數(shù)');
});
為多個(gè)ajax指定回調(diào)函數(shù)
我們可以通過when方法,為多個(gè)事件指定一個(gè)回調(diào)函數(shù)
$.when($.ajax('test.html'), $.ajax('test2.html')).done(function (res) {
console.log('數(shù)據(jù)讀取成功');
}).fail(function () {
console.log('數(shù)據(jù)讀取失敗');
});
為普通函數(shù)指定回調(diào)函數(shù)
前面說到的when是用于ajax方法,而ajax其實(shí)是deferred對(duì)象,如果不是ajax對(duì)象,換成普通的函數(shù)呢,直接使用when是不會(huì)有效果的,會(huì)直接執(zhí)行done方法
所以我們需要手動(dòng)新建一個(gè)deferred對(duì)象
var defer = $.deferred(); //新建一個(gè)deferred對(duì)象
var wait = function (defer) {
var tasks = function () {
console.log('執(zhí)行完畢!');
defer.resolve(); //改變deferred對(duì)象的執(zhí)行狀態(tài) - 成功
};
setTimeout(tasks, 5000);
return defer;
};
這里的resolve就是觸發(fā)done的,對(duì)應(yīng)的reject方法則是用來調(diào)用fail方法的。
var defer = $.deferred(); //新建一個(gè)deferred對(duì)象
var wait = function (defer) {
var tasks = function () {
console.log('執(zhí)行完畢!');
defer. reject(); //改變deferred對(duì)象的執(zhí)行狀態(tài) - 失敗
};
setTimeout(tasks, 5000);
return defer;
};
執(zhí)行方法
$.when(wait(defer)).done(function (res) {
console.log('數(shù)據(jù)讀取成功');
}).fail(function () {
console.log('數(shù)據(jù)讀取失敗');
});
進(jìn)一步優(yōu)化
上面的代碼還有一些問題,defer是暴露在全局的,所以我們是可以通過在全局進(jìn)行defer.resolve()來提前回調(diào)。
為了避免這種情況,jQuery提供了deferred.promise()方法,它的作用是在原來的deferred對(duì)象上返回另一個(gè)deferred對(duì)象,后者只開放與改變執(zhí)行狀態(tài)無關(guān)的方法(比如done方法和fail方法)屏蔽與改變執(zhí)行狀態(tài)有關(guān)的方法(比如resolve和reject方法)。
var defer = $.deferred(); //新建一個(gè)deferred對(duì)象
var wait = function (defer) {
var tasks = function () {
console.log('執(zhí)行完畢!');
defer.resolve(); //改變deferred對(duì)象的執(zhí)行狀態(tài) - 成功
};
setTimeout(tasks, 5000);
return defer.promise();
};
$.when(wait(defer)).done(function (res) {
console.log('數(shù)據(jù)讀取成功');
}).fail(function () {
console.log('數(shù)據(jù)讀取失敗');
});
也可以把defer包在函數(shù)中
// 普通方法
var wait = function () {
var defer = $.deferred(); //新建一個(gè)deferred對(duì)象
var tasks = function () {
console.log('執(zhí)行完畢!');
defer.resolve(); //改變deferred對(duì)象的執(zhí)行狀態(tài) - 成功
};
setTimeout(tasks, 5000);
return defer.promise();
};
$.when(wait()).done(function (res) {
console.log('數(shù)據(jù)讀取成功');
}).fail(function () {
console.log('數(shù)據(jù)讀取失敗');
});
// 執(zhí)行異步
var setAjax = function () {
var defer = $.Deferred();
if (xhr) {
xhr.abort();
xhr = null;
}
var xhr = $.ajax({
url: 'test.html',
success: function (res) {
console.log('數(shù)據(jù)讀取成功');
defer.resolve(res);
},
error: function () {
console.log('數(shù)據(jù)讀取失敗');
defer.reject();
}
});
return defer.promise();
}
$.when(setAjax()).then(function (res) {
console.log('數(shù)據(jù)讀取成功', res);
}, function () {
console.log('數(shù)據(jù)讀取失敗');
});
以上所述是小編給大家介紹的jQuery中的derferred使用方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jQuery Uploadify 上傳插件出現(xiàn)Http Error 302 錯(cuò)誤的解決辦法
本文給大家介紹jQuery Uploadify 上傳插件出現(xiàn)Http Error 302 錯(cuò)誤的解決辦法,涉及到uploadify上傳錯(cuò)誤302相關(guān)問題,對(duì)本文感興趣的朋友一起看看吧2015-12-12
jQuery progressbar通過Ajax請(qǐng)求實(shí)現(xiàn)后臺(tái)進(jìn)度實(shí)時(shí)功能
這篇文章主要介紹了jQuery progressbar通過Ajax請(qǐng)求實(shí)現(xiàn)后臺(tái)進(jìn)度實(shí)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
getJSON調(diào)用后臺(tái)json數(shù)據(jù)時(shí)函數(shù)被調(diào)用兩次的原因猜想
近期在做前端開發(fā)時(shí)候使用到getJSON調(diào)用后臺(tái)json數(shù)據(jù),發(fā)現(xiàn)后臺(tái)的函數(shù)被調(diào)用兩次,函數(shù)名稱為getMessages,下面是本人的一些猜想,感興趣的朋友可以參考下2013-09-09
jquery遍歷checkbox的注意事項(xiàng)說明
本篇文章主要是對(duì)jquery遍歷checkbox的注意事項(xiàng)進(jìn)行了說明介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02
jquery 模式對(duì)話框終極版實(shí)現(xiàn)代碼
今天終于有時(shí)間把我以前寫的一個(gè)jQuery插件進(jìn)行整理,改進(jìn)。這是一個(gè)模擬“模式對(duì)話框”的插件,該模式對(duì)話框共有三種皮膚,紅、綠、藍(lán)。2009-09-09
jQuery 擴(kuò)展對(duì)input的一些操作方法
擴(kuò)展對(duì)input的一些方法(練習(xí)jQuery插件)2009-10-10
jquery實(shí)現(xiàn)漂亮的二級(jí)下拉菜單代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)漂亮的二級(jí)下拉菜單代碼,涉及jquery鼠標(biāo)click事件控制頁面class屬性動(dòng)態(tài)變換效果的切換技巧,非常美觀實(shí)用,需要的朋友可以參考下2015-08-08
jQuery 獲取跨域XML(RSS)數(shù)據(jù)的相關(guān)總結(jié)分析
下面小編就為大家?guī)硪黄猨Query 獲取跨域XML(RSS)數(shù)據(jù)的相關(guān)總結(jié)分析。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05

