当连续进行多个请求,并且请求的url地址相同时。放弃前面的所有请求,只执行最后一次请求。

// 当连续进行多个请求,并且请求的url地址相同时。放弃前面的所有请求,只执行最后一次请求。
function ajaxFilter(){
var pendingRequests = {};
jQuery.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
var key = options.url;
console.log(key);
if (!pendingRequests[key]) {
pendingRequests[key] = jqXHR;
}else{
//jqXHR.abort(); //放弃后触发的提交
pendingRequests[key].abort(); // 放弃先触发的提交
}
 
var complete = options.complete;
options.complete = function(jqXHR, textStatus) {
pendingRequests[key] = null;
if (jQuery.isFunction(complete)) {
complete.apply(this, arguments);
}
};
});
}
原文地址:https://www.cnblogs.com/amaoegg/p/10675383.html