2017-01-03 90 views
0

我想循環一個數組如果是結果,將這個結果推入一個javascript數組,並將其從每個循環和ajax調用中取出。怎麼樣?推送到jQuery數組每個循環和jQuery ajax調用

我想是這樣的:

var ides = ["2254365", "2255017", "2254288", "2220745", "2254452", "2255239", "2232426", "2255143", "2248513", "2254295", "2233629", "2238651", "2254901", "2238430", "2239471", "2255294", "2217888", "2242302", "2242310", "2220380", "56121236", "2255244", "2235716", "2246897"]; 
 
var iArray = []; 
 
$.each(ides, function(index, woide) { 
 
    $.ajax({ 
 
     url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20=%20" + woide + "&format=json", 
 
     dataType: 'json', 
 
     success: function(data) { 
 
      if (data.query.results != null) { 
 
       iArray.push(woide+': '+data.query.results.channel.item.condition.code); 
 
      } 
 
     } 
 
    }) 
 
}) 
 
console.log(iArray); //this don't work
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+2

因爲它在每個循環結束後立即運行。它不會等待ajax調用完成。 – Jai

+0

AJAX是一個異步調用,因此當您在成功/錯誤回調之外執行代碼時,它可能尚未完成。 –

回答

1

你的Ajax調用是異步的,因此會需要一些時間來填寫您所選擇的陣列。但是在Ajax完成之前,每個循環都完成了迭代,你的日誌調用就會觸發。

此時ajax還在進行中。

你必須移動log了ajax的成功處理程序中:

var ides = ["2254365", "2255017", "2254288", "2220745", "2254452", "2255239", "2232426", "2255143", "2248513", "2254295", "2233629", "2238651", "2254901", "2238430", "2239471", "2255294", "2217888", "2242302", "2242310", "2220380", "56121236", "2255244", "2235716", "2246897"]; 
 
var iArray = []; 
 
$.each(ides, function(index, woide) { 
 
    $.ajax({ 
 
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20=%20" + woide + "&format=json", 
 
    dataType: 'json', 
 
    success: function(data) { 
 
     if (data.query.results != null) { 
 
     iArray.push(woide + ': ' + data.query.results.channel.item.condition.code); 
 
     } 
 

 
     if (index === ides.length - 1) { 
 
     console.log(JSON.stringify(iArray, 0, 0)); // <-----move it here. 
 
     } 
 
    } 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

可能是OP想要在所有值被壓入後僅打印一次。爲什麼不使用標誌來查看'push'是否完整,然後打印? :) –

+0

它每次循環迭代。 @Me hdi:請詳細說明要求。 – Krunal

+1

@GuruprasadRao也許你是對的。因爲這個OP可以使用索引來查看是否是'index === ides.length-1'。 – Jai

0

嘗試通過Ajax調用返回再後來,你在不同的循環調用店承諾

var ides = ["2254365", "2255017", "2254288", "2220745", "2254452", "2255239", "2232426", "2255143", "2248513", "2254295", "2233629", "2238651", "2254901", "2238430", "2239471", "2255294", "2217888", "2242302", "2242310", "2220380", "56121236", "2255244", "2235716", "2246897"]; 
 
var pArray = []; 
 
var iArray = []; 
 
$.each(ides, function(index, woide) { 
 
    var promis = $.ajax({ 
 
     url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20=%20" + woide + "&format=json", 
 
     dataType: 'json', 
 
    }); 
 
\t 
 
\t pArray.push(promis); 
 
}) 
 

 
$.each(pArray, function(index,prom){ 
 
\t 
 
\t prom.done(function(data){ 
 
\t \t if (data.query.results != null) { 
 
       iArray.push(data.query.results.channel.item.condition.code); 
 
     } 
 
\t }); 
 

 
});

+0

超過兩個不同循環的優勢。 – Jai