Javascript 使用异步AJAX(使用jQuery)请求加载数据时覆盖变量

Javascript 使用异步AJAX(使用jQuery)请求加载数据时覆盖变量,javascript,jquery,ajax,json,asynchronous,Javascript,Jquery,Ajax,Json,Asynchronous,我有以下代码,可以在需要时提前一个月加载有关用户付款的信息。当我每个月等待加载时,该函数都会工作,但是当运行多个ajax请求时,它会失败。基本上,重复的数据被添加到我的monthData数组中,这意味着loadedPaymentMonth在每次函数调用中都被覆盖。我想异步加载数据,有什么想法吗 这不起作用: var monthData=[]; function loadMonth(monthToLoad,curMonth){ $("#paymentContent").find(".lo

我有以下代码,可以在需要时提前一个月加载有关用户付款的信息。当我每个月等待加载时,该函数都会工作,但是当运行多个ajax请求时,它会失败。基本上,重复的数据被添加到我的monthData数组中,这意味着loadedPaymentMonth在每次函数调用中都被覆盖。我想异步加载数据,有什么想法吗

这不起作用:

var monthData=[]; 
function loadMonth(monthToLoad,curMonth){
    $("#paymentContent").find(".loader").remove();
    var monthToGet;
    switch(monthToLoad){
        case "Next": monthToGet=monthData.length-curMonth+1;
        break;
        case "Prev": monthToGet=-curMonth-1;
        break;
        case "This": monthToGet=currentMonthPointer-curMonth;
        break;
    }
    var loadedPaymentMonth=new PaymentMonth();
    if(monthToGet<0){
        currentMonthPointer+=1;
        monthData.unshift(loadedPaymentMonth);
    }
    else{
        monthData.push(loadedPaymentMonth);
    }
    $.getJSON(host+"GetDataForMonth.aspx?StartMonth="+monthToGet,function(data){
        loadedPaymentMonth.setMonthData(data.monthData,data.month,data.year);
        if($("#paymentsContent").find(".loader").remove().length>0){
            loadedPaymentMonth.createPaymentRowsForMonth();
        }
    }).error(function(){
        $("#paymentsContent").find(".loader").remove();
    });
}
var monthData=[];
功能加载月(月加载,curMonth){
$(“#paymentContent”).find(“.loader”).remove();
var Monhtoget;
开关(月负荷){
案例“下一个”:monthToGet=monthData.length curMonth+1;
打破
案例“Prev”:monthToGet=-curMonth-1;
打破
案例“This”:monthToGet=currentMonthPointer curMonth;
打破
}

var loadedPaymentMonth=新的PaymentMonth(); 如果(每月0日){ loadedPaymentMonth.createPaymentRowsForMonth(); } }).错误(函数(){ $(“#支付内容”).find(“.loader”).remove(); }); }
这是:

function loadMonth(monthToLoad,curMonth){
    $("#paymentContent").find(".loader").remove();
    var monthToGet;
    switch(monthToLoad){
        case "Next": monthToGet=monthData.length-curMonth+1;
        break;
        case "Prev": monthToGet=-curMonth-1;
        break;
        case "This": monthToGet=currentMonthPointer-curMonth;
        break;
    }
    var loadedPaymentMonth=new PaymentMonth();
    if(monthToGet<0){
        currentMonthPointer+=1;
        monthData.unshift(loadedPaymentMonth);
    }
    else{
        monthData.push(loadedPaymentMonth);
    }
    $.ajax({
        url:host+"GetDataForMonth.aspx?StartMonth="+monthToGet,
        async:false,
        dataType:"json",
        success:function(data){
        loadedPaymentMonth.setMonthData(data.monthData,data.month,data.year);
        if($("#paymentsContent").find(".loader").remove().length>0){
            loadedPaymentMonth.createPaymentRowsForMonth();
        }
        }
    }).error(function(){
        $("#paymentsContent").find(".loader").remove();
    });
}
函数加载月(monthToLoad,curMonth){
$(“#paymentContent”).find(“.loader”).remove();
var Monhtoget;
开关(月负荷){
案例“下一个”:monthToGet=monthData.length curMonth+1;
打破
案例“Prev”:monthToGet=-curMonth-1;
打破
案例“This”:monthToGet=currentMonthPointer curMonth;
打破
}

var loadedPaymentMonth=新的PaymentMonth(); 如果(每月0日){ loadedPaymentMonth.createPaymentRowsForMonth(); } } }).错误(函数(){ $(“#支付内容”).find(“.loader”).remove(); }); }
所以我用代码检查了一个月是否加载了,如果没有,它就加载了。然而,我并没有处理这个案例。因为paymentmonth是在AJAX启动之前添加的,所以我需要用一个新的paymentmonth对象替换旧的paymentmonth对象(如果新请求在旧请求之前完成,则仅使用旧的paymentmonth对象也会导致同步问题)。因此:

if(monthToGet<0){
    currentMonthPointer+=1;
    monthData.unshift(loadedPaymentMonth);
}
else{
    monthData.push(loadedPaymentMonth);
}

if(monthToGetvar loadedPaymentMonth=new PaymentMonth();创建一个新的PaymentMonth对象并将其插入数组。每个函数调用都应创建一个新的PaymentMonth对象。因此,虽然变量名称可能相同,但引用应特定于创建它的函数上下文。
var loadedPaymentMonth=new PaymentMonth();
if(monthToLoad!="This"){
    if(monthToGet<0){
        currentMonthPointer+=1;
        monthData.unshift(loadedPaymentMonth);
    }
    else{
        monthData.push(loadedPaymentMonth);
    }
}
else{
    monthData[currentMonthPointer]=loadedPaymentMonth;
}