Ajax loop call problem, very anxious, ask the seniors to have a look

I need to get monthly statistics, and then the statistical cloud returns daily. So I passed the reference time for 12 months respectively. Call 12 times to get the total count for each month. Each time the resulting value is assigned to an array month. This array holds the total for 12 months. But because it"s an asynchronous request, I can"t get the variables inside the array outside. After I add async to ajax, the page loads very slowly.

var month = [];
    getAppStatisticDataById(0,"2018-1-1","2018-1-31")
    getAppStatisticDataById(1,"2018-2-1","2018-2-28")
    getAppStatisticDataById(2,"2018-3-1","2018-3-31")
    getAppStatisticDataById(3,"2018-4-1","2018-1-30")
    getAppStatisticDataById(4,"2018-5-1","2018-1-31")
    getAppStatisticDataById(5,"2018-6-1","2018-1-30")
    getAppStatisticDataById(6,"2018-7-1","2018-1-31")
    getAppStatisticDataById(7,"2018-8-1","2018-1-31")
    getAppStatisticDataById(8,"2018-9-1","2018-1-30")
    getAppStatisticDataById(9,"2018-10-1","2018-1-31")
    getAppStatisticDataById(10,"2018-11-1","2018-1-30")
    getAppStatisticDataById(11,"2018-12-1","2018-1-31")
    
    function getAppStatisticDataById(index,start,end) {
        $.ajax({
            url: "https://r.apicloud.com/analytics/getAppStatisticDataById",
            type: "post",
            timeout: 10000, //  10 
            async :false,
            headers: {
                "X-APICloud-AppId": "xxxxxxxxx",
                "X-APICloud-AppKey": varappKey,
            },
            dataType: "json",
            data: {

                    startDate: start,
                    endDate: end
            },
            success: function(data) {
                if(data.st == 1){
                  var count = 0;
                  for(let i = 0;i< data.msg.length; iPP){
                  //
                    count += data.msg[i].newRegsCount
                  }
                  //month
                  month[index] = count;
                }
            },
            error: function(err) {
              console.log(JSON.stringify(err)+"err")
            },
            complete: function(XMLHttpRequest, status) { //
                // alert(JSON.stringify(XMLHttpRequest))
            }
        })
    }

you can use Promise.all, to simulate Promise.all if ES6, is not supported.

function allAjax(arr,callback){
    var count = 0,month = [];
    arr.map(function(item,i){
        $.ajax({
            ...
            success:function(data){
                month.push({
                    month:item,
                    data:data
                })
            },
            complete: function(){
                count PP ;
                if(count==arr.length){
                    callback && callback(month)
                }
            }
        })
    })
}
var arr = [1,2,3]
allAjax(arr,function(month){
    console.log(month)
})

var month = [];
$.when(
  getAppStatisticDataById(0, '2018-1-1', '2018-1-31'),
  getAppStatisticDataById(1, '2018-2-1', '2018-2-28'),
  getAppStatisticDataById(2, '2018-3-1', '2018-3-31'),
  getAppStatisticDataById(3, '2018-4-1', '2018-1-30'),
  getAppStatisticDataById(4, '2018-5-1', '2018-1-31'),
  getAppStatisticDataById(5, '2018-6-1', '2018-1-30'),
  getAppStatisticDataById(6, '2018-7-1', '2018-1-31'),
  getAppStatisticDataById(7, '2018-8-1', '2018-1-31'),
  getAppStatisticDataById(8, '2018-9-1', '2018-1-30'),
  getAppStatisticDataById(9, '2018-10-1', '2018-1-31'),
  getAppStatisticDataById(10, '2018-11-1', '2018-1-30'),
  getAppStatisticDataById(11, '2018-12-1', '2018-1-31')
).then(function() {
  [].slice.call(arguments).forEach(function(data, index) {
    if (data.st == 1) {
      var count = 0;
      for (let i = 0; i < data.msg.length; iPP) {
        //
        count += data.msg[i].newRegsCount
      }
      //month
      month[index] = count;
    }
  })
})

function getAppStatisticDataById(index, start, end) {
  return $.ajax({
    url: 'https://r.apicloud.com/analytics/getAppStatisticDataById',
    type: 'post',
    timeout: 10000, //  10 
    async: false,
    headers: {
      'X-APICloud-AppId': 'xxxxxxxxx',
      'X-APICloud-AppKey': varappKey,
    },
    dataType: "json",
    data: {

      startDate: start,
      endDate: end
    },
    error: function (err) {
      console.log(JSON.stringify(err) + 'err')
    }
  })
}
Menu