怎样防止重复发送 Ajax 请求


//ajax 重复提交 只要组后一次的结果!
window.pendingRequests = {};
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    var key = options.url+Math.random();
    if (!pendingRequests[key] && key.indexOf('.html') == -1) {
        pendingRequests[key] =jqXHR;
    }
    for(let i in pendingRequests)
        if(i!=key){
            pendingRequests[i].abort()
        }
    var complete = options.complete;
    options.complete = function (jqXHR, textStatus) {
        delete pendingRequests[key];
        if ($.isFunction(complete)) {
            complete.apply(this, arguments);
        }
    };
});
ajax 重复提交  当只需要第一次ajaxwindow.pendingRequests = {};
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    var key=options.url;
    if (!pendingRequests[key] && key.indexOf('.html') == -1) {
        pendingRequests[key] =jqXHR;
    } else if (key.indexOf('.html') == -1) {
        jqXHR.abort();
    }
    var complete = options.complete;
    options.complete = function (jqXHR, textStatus) {
        delete pendingRequests[key];
        if ($.isFunction(complete)) {
            complete.apply(this, arguments);
        }
    };
});

你可能感兴趣的:(js)