我使用ajaxQueue的例子Queueing something like Ajax Calls提到:jQuery的,出列的ajaxQueue
// jQuery on an empty object, we are going to use this as our queue
var ajaxQueue = $({});
$.ajaxQueue = function(ajaxOpts) {
// Hold the original complete function.
var oldComplete = ajaxOpts.complete;
// Queue our ajax request.
ajaxQueue.queue(function(next) {
// Create a complete callback to fire the next event in the queue.
ajaxOpts.complete = function() {
// Fire the original complete if it was there.
if (oldComplete) {
oldComplete.apply(this, arguments);
}
// Run the next query in the queue.
next();
};
// Run the query.
$.ajax(ajaxOpts);
});
};
我也有一個功能,使AJAX調用,並將結果追加到一個div(簡體):
function ajaxCall() {
$.ajaxQueue({
type: "POST",
url: myURL,
async: true,
cache: false,
success: function(result) {
$('#divID').append($('<div/>').html($(result).html()).fadeIn(200));
}
});
}
然後通過Ajax調用click事件我環路(簡體):
$("#btn").on("click", function() {
// (???) Dequeue the ajaxQueue
$('#divID').html(''); // Clear current results
for(var i=0; i<15; i++) {
ajaxCall();
}
});
問題
如果用戶在隊列仍在運行時單擊鏈接,則會添加一個新的ajax調用隊列,從而產生比預期更多的結果。在新的循環開始之前,我需要清除點擊隊列。
這裏是jsFiddle Demo。 非常感謝任何建議。
因此,當用戶第二次點擊鏈接時,第一次點擊已經檢索到的結果會發生什麼? –
@rahulmaindargi我只想結果被清除(刪除)。 –