Corey Frang在stackoverflow上發佈了一個有用的插件,用於排隊ajax請求。就我而言,用「大塊」加載大量數據是非常有用的,以減少用戶感知的慢度。除非在ajax請求的隊列完成之前觸發分頁事件,否則這種情況完美地起作用,在這種情況下,持有加載對象的容器將清除,但排隊的請求將繼續運行,從而導致不希望的行爲。我想要的是找到/學習清除所有現有請求隊列的方法。開發人員發佈了一種方法來執行此操作,但它似乎不起作用。我試着在jQuery網站上閱讀.queue,但我似乎無法理解如何使這項工作。我已經花了很多時間來解決這個問題,但也許我對jQuery的一些更復雜的方面的熟悉程度讓我難以接受。希望有人在那裏誰更熟悉的jQuery能幫助:)(標記什麼建議並沒有出現一些工作「!!!!!」)jQuery「隊列」(ajaxQueue插件) - 如何清除?
/*
* jQuery.ajaxQueue - A queue for ajax requests
*
* (c) 2011 Corey Frang
* Dual licensed under the MIT and GPL licenses.
*
* Requires jQuery 1.5+
*/
(function($) {
// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $({});
$.ajaxQueue = function(ajaxOpts) {
var jqXHR,
dfd = $.Deferred(),
promise = dfd.promise();
// queue our ajax request
ajaxQueue.queue(doRequest);
// add the abort method
promise.abort = function(statusText) {
// proxy abort to the jqXHR if it is active
if (jqXHR) {
return jqXHR.abort(statusText);
}
// if there wasn't already a jqXHR we need to remove from queue
var queue = ajaxQueue.queue(),
index = $.inArray(doRequest, queue);
if (index > -1) {
queue.splice(index, 1);
}
// and then reject the deferred
dfd.rejectWith(ajaxOpts.context || ajaxOpts,
[ promise, statusText, "" ]);
return promise;
};
// run the actual query
function doRequest(next) {
jqXHR = $.ajax(ajaxOpts)
.then(next, next)
.done(dfd.resolve)
.fail(dfd.reject);
}
return promise;
};
// !!!!!!!!!!!!!!!!!
// this is what the developer suggested on his website in the comments section
// ... but it does not appear to work
$.ajaxQueue.stop = function(clear) { ajaxQueue.stop(clear); }
})(jQuery);
的....? – Luceos 2012-07-24 12:15:55
不完整的問題? :) – Dipak 2012-07-24 12:16:04
我的壞傢伙 - 意外地在輸入標籤時點擊「輸入」鍵,現在將其他標籤貼出:) – Eva 2012-07-24 12:23:15