ajax防止重复提交
//防止重复提交
var pendingRequests = {};
jQuery.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
var key = options.url;
i=0;
i++;
console.log(key+"aaaaaaaaaaa"+i);
if (!pendingRequests[key]) {
pendingRequests[key] = jqXHR;
}else{
//jqXHR.abort(); //放弃后触发的提交
setTimeout(function(){console.log("放弃先触发的提交");},1);
pendingRequests[key].abort(); // 放弃先触发的提交
}
var success = options.success;
options.success = function(jqXHR, textStatus) {
pendingRequests[key] = null;
if (jQuery.isFunction(success)) {
success.apply(this, arguments);
}
};
var error = options.error;
options.error = function(jqXHR, textStatus) {
pendingRequests[key] = null;
if (jQuery.isFunction(error)) {
error.apply(this, arguments);
}
};
});
var pendingRequests = {};
jQuery.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
var key = options.url;
i=0;
i++;
console.log(key+"aaaaaaaaaaa"+i);
if (!pendingRequests[key]) {
pendingRequests[key] = jqXHR;
}else{
//jqXHR.abort(); //放弃后触发的提交
setTimeout(function(){console.log("放弃先触发的提交");},1);
pendingRequests[key].abort(); // 放弃先触发的提交
}
var success = options.success;
options.success = function(jqXHR, textStatus) {
pendingRequests[key] = null;
if (jQuery.isFunction(success)) {
success.apply(this, arguments);
}
};
var error = options.error;
options.error = function(jqXHR, textStatus) {
pendingRequests[key] = null;
if (jQuery.isFunction(error)) {
error.apply(this, arguments);
}
};
});
注意:ajax必须是异步时,这段代码才有效果
学而不思则罔,思而不结则殆,结而不看,一事无成