2015-10-14 27 views
0

嗯,我有一个“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。

回答

2

您可以试试。我已经删除了for循环,并且只有在前一次调用成功后才启动索引增加的下一个调用。

// This list will always have 40 items/values. 
var ids = [12,3453,234,743,235,433, ..]; 
var index = 0; 
RequestAjax(ids,index); 

function RequestAjax(ids,index) 
{  
    if(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); 
      RequestAjax(ids,index++); 
     }, 
     async:true // See here, this is a Async request 
    }); 
    } 
} 
+0

但这个同时发送... –

+0

不是真的。每次只有数组被传递(无论如何是全局的)并且索引从0更新为1,并且只有当响应返回时才再次调用相同的函数。 – DinoMyte

+0

那么,现在我明白了这个概念。谢谢 –

1

我觉得DinoMyte的回答很有用。 根据@DinoMyte的回答,您还可以使用.push().shift()来操作数组ids

// This list will always have 40 items/values. 
var ids = []; 
Manipulate([12,3453,234,743,235,433]); 

function RequestAjax() 
{  
    if(ids.length > 0) 
    { 
    var id = ids.shift(); 
    // This request take long time(40-60 seconds) 
    $.ajax({ 
     url  : 'http://website.com/id=' + id, 
     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); 
      RequestAjax(); 
     }, 
     async:true // See here, this is a Async request 
    }); 
    } 
} 

function Manipulate(id) 
{ 
    var shouldRequest = false; 
    if(0 === ids.length){ 
     shouldRequest = true; 
    } 

    if(toString.apply(id) === '[object Array]'){ 
     ids = ids.concat(id); 
    }else if('number' === typeof id){ 
     ids.push(id); 
    } 

    if(shouldRequest === true && ids.length > 0){ 
     RequestAjax(); 
    } 
} 
+0

我不明白这个代码...这有“ids.contact”的方法,但这不存在...我不明白这个概念..你能告诉我它是如何工作的吗?感谢太多 –

+0

@OlafErlandsen对不起。我总是把'concat'写成'contact'。我已经纠正它。 – liuzeyafzy

+0

嗯,我尝试使用u代码,但是......没有任何反应。 我将我的测试代码粘贴到JSFiddle中。请记住在浏览器中使用“javascript控制台”。 我使用Google Chrome。 JSFiddle:https://jsfiddle.net/aeqe27os/ –