账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ajax循环调用问题,很着急,求前辈看看
    89
    0

    我需要获取每个月的统计数据,然后统计云返回的是每天的。所以我分别传参12个月的时间过去。调用12次,得到每个月的总数count。每次得到的值都赋值给一个数组month。这个数组保存12个月的总数。但是因为是异步请求,所以我在外面取不到数组里面变量。我给ajax加上async以后,页面会很慢加载。

    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; i++){
                      //循环把每天的数据加起来
                        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)) 
                }
            })
        }
    1
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部