jQuery请求一个url列表,同时限制最大并发请求数

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个请求,当他们每个人得到响应时,我发送另一个请求.

Jas*_*per 0

这应该可以解决问题:

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)