嗯,我有一个“ID”列表,我需要为每个“ID”发送一个Ajax请求。 IDE是当请求完成时,运行以下Ajax请求。 所有这些与异步请求,因为否则,浏览器字面上“停止”,直到请求完成加载。如何通过jQuery和Queue发送ajax请求?
逻辑很简单: 我们在循环中创建一个循环,并为每个ID创建一个asinconica的Ajax请求。 问题是,异步Ajax请求阻止我访问全局变量ID。
例子:
// This list will always have 40 items/values.
var ids = [12,3453,234,743,235,433, ..];
for (var index = 0; index < ids.length; index++) {
// This request take long time(40-60 seconds)
$.ajax({
url : 'http://website.com/id=' + ids[index], // See here how to call to global variable ids
success : function (response) {
// And here, i call the element '#element' and get and set the 'value text'
var currentTotal = parseInt($('#element').text());
$'#element').text(currentTotal + response.total);
},
async:true // See here, this is a Async request
});
}
注意,每个请求都必须以前加载完成后发送,将永远是一个动态的名单,因为有时我会送不同的ID。
但这个同时发送... –
不是真的。每次只有数组被传递(无论如何是全局的)并且索引从0更新为1,并且只有当响应返回时才再次调用相同的函数。 – DinoMyte
那么,现在我明白了这个概念。谢谢 –