我在我的应用程序中进行了大量的API调用,比如50次。
完成所有api调用的总时间约为1分钟。所有api调用的优先级将为2。我已经启用了角度缓存。
因此,在此期间,如果我的应用程序的用户只想关注所有api调用中的一些,即只有6个api调用。
然后,我再一次预测优先级为1的6个api调用。
但是我还是没有达到我的目标?也就是说,这6个api调用需要尽快接收数据。
请参考下面的示例代码。
在初始加载时:
for(var i=1,priority=19;i<=19,priority>=1;i++,priority--)
{
$http.get("http://localhost:65291/WebService1.asmx/HelloWorld"+i+"?test=hari",{priority:2})
.then(function(response) { });
}
}
在某些事件中,单击:
$http.get("http://localhost:65291/WebService1.asmx/HelloWorld7?test=hari",{priority:1})
.then(function(response) { });
}
发布于 2017-03-23 06:47:05
如果您想一次发送多个http请求,请使用$q.all
在循环内部,将http请求推送到一个数组,并立即发送该http数组。
var httpArr = []
for (var i = 1, priority = 19; i <= 19, priority >= 1; i++, priority--) {
httpArr.push($http.get("http://localhost:65291/WebService1.asmx/HelloWorld" + i + "?test=hari", {
priority: 2
}))
}
$q.all(httpArr).then(function(response) {
console.log(response[0].data) //1st request response
console.log(response[1].data) //2nd request response
console.log(response[2].data) //3rd request response
})
https://stackoverflow.com/questions/42968892
复制