won*_*ng2 5 javascript ajax performance jquery
可能重复:
队列AJAX调用
我有一个id列表:
var ids = [3738, 75995, 927, ... ]; // length is about 2000
我想请求中的URL http://xx/ + id与$.getJSON,如:
ids.forEach(function(id, index){
$.getJSON('http://xx/' + id, function(data){
// store the data in another array.
});
});
Run Code Online (Sandbox Code Playgroud)
但是,这会一次发出太多请求,使浏览器暂停一段时间,所以我的问题是,我怎样才能限制jQuery中并发ajax请求的数量?例如,我发送10个请求,当他们每个人得到响应时,我发送另一个请求.
这应该可以解决问题:
var current;
function fetchCurrentLast()
{
if (current < ids.length)
{
var id = ids[current];
current++;
$.getJSON('http://xx/' + id, function(data){
// store the data in another array.
fetchCurrentLast();
});
}
}
current = 0;
for (var i = 0; i < 10; i++)
{
fetchCurrentLast();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1110 次 |
| 最近记录: |