我需要获取每个月的统计数据,然后统计云返回的是每天的。所以我分别传参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))
}
})
}