jQuery的限制同时AJAX请求数 [英] jQuery Limit The Number Of Simultaneous AJAX Requests
问题描述
让我有一系列的AJAX事件,我想火,但我想同时请求的数量限制为5,而排队的其余部分。因此,例如,我有以下几点:
so I have a series of AJAX events I would like to fire, but I want to limit the number of simultaneous requests to 5, and queue up the rest. So for example, I have the following:
$("div.server").each(function() {
$.ajax(....);
});
所以有可能是100的div类服务器
,但我想只能运行在任何特定时间5的请求。一旦请求完成后,应该进入下一个。
So there may be 100 divs with the class server
, but I want to only run 5 requests at any given time. Once a request finishes it should proceed to the next.
什么是做到这一点的最好方法是什么?
What is the best way to do this?
推荐答案
要做到这一点是让浏览器处理它的最好方法。通常的浏览器已经拥有了每个主机连接的限制,使他们自动排队连接,如果有太多的。
The best way to do this is letting the browser deal with it. Usually browsers already have a per-host connection limit so they will automatically queue connections if there are too many.
不过,我会考虑更改API,因此它需要时间,从多个元素,并返回数据的多个元素 - 即减少必要的HTTP请求的整体计数
However, I'd consider changing the API so it takes date from multiple elements and also returns data for multiple elements - i.e. reducing the overall count of HTTP requests necessary.
这篇关于jQuery的限制同时AJAX请求数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!