我只是最近才瞭解到jquery/javascript,而且我正面臨以下問題。for循環/遞歸中的執行順序
我想控制一個嵌套循環中的函數調用序列和/或在jquery/javascript中的遞歸。在我看來,現在所有函數幾乎同時被調用,並且不像我習慣於其他編程語言(如R)那樣遵循代碼中的順序。在R腳本中,只要代碼不會處理下一行當前的代碼行是未完成的。但似乎jQuery代碼同時觸發我的所有getJSON請求,並在1個結果可用時立即處理結果。它不關注getJSON調用的順序。這是真的,還是我錯過了什麼?幾次刷新頁面給我的結果集以不同的順序,而相同的順序會預期,一次又一次...
爲了解釋上述我創建了以下可行代碼,它使用api gridpointweather.com的插圖目的。
<!DOCTYPE html>
<meta charset="utf-8">
<!--Body-->
<body>
<div id="results"></div>
</body>
<!--Load jquery + uri.js-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.17.0/URI.min.js"></script>
<script>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var g_asLoc = ["24", "42", "19"];
var g_asWeather = [];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getWeatherInfo(location, offset){
console.log("Location: " + location);
console.log("Offset: " + offset);
var sUrl = 'http://api.gridpointweather.com/weather/getjson?location=' + location + '%2C-72&model=gfs.api0.n7m4csjf3x2s92ic&hours=24&inunits=1&interptype=2&offset=' + offset;
var requestUrl = sUrl;
$.getJSON(requestUrl, function(data) {
try {
console.log("Data for location " + location, data);
}catch(err) {
console.log(err);
}
// Store something.
$.each(data.data, function(index, weatherInfo){
g_asWeather.push("Object " + index + ", Location: " + location + ", rawdate: " + weatherInfo.rawdate + ", Temp: " + weatherInfo.temp + "<br>");
});
// Offset with 25 if condition not met.
if(offset == -25){
// Display in #results.
g_asWeather.push("<br><br>");
$("#results").html(g_asWeather.join('\n'));
console.log("Finished for location " + location);
return;
}else{
console.log("Running again using offset " + (offset-25) + " for location " + location);
getWeatherInfo(location, offset - 25);
}
}).error(function(){
console.log("JSON error!");
return;
});
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var iCounter = 0;
while(iCounter < 2){
for(iLoc=0; iLoc<g_asLoc.length; iLoc++){
getWeatherInfo(g_asLoc[iLoc], 0);
}
iCounter = iCounter + 1;
}
</script>
最好我想代碼調用的函數getWeatherInfo 1個位置,然後下一個位置繼續之前完成此地區的遞歸...遞歸的順序也很重要,因爲結果from offset = 0應該在g_asWeather之前,offset = 25的結果之前。在完成所有位置之後,代碼應等待,比如1000 ms,然後再將iCounter加1並重復for循環,而iCounter < 2.總結結果集應包含:iCounter = 0,位置24,偏移量= 0,偏移量= 25,位置42,偏移量= 0,偏移量= -25,位置19,偏移量= 0,偏移量= -25,等待1000ms,iCounter = 1,位置24,偏移量= 0,偏移量= -25等等等等。
非常感謝。
while和for循環是同步的(按順序發生)但'getWeatherInfo'函數內的幾個語句是異步的(發生亂序)。這裏是一個[非常基本的入門](http://rowanmanning.com/posts/javascript-for-beginners-async/)同步vs異步javascript。更具體地說,'$ .getJSON'和'$ .each'函數都是異步的。基本上,只要解析器到達'$ .getJSON'函數,getWeatherInfo函數就會返回,for循環的下一次迭代將繼續。 –
這就是我們承諾的原因。使異步程序流的工作變得容易。 – ste2425
@MichaelL。這有助於理解爲什麼會出現這種情況。但是我必須確保for循環中的所有內容都是同步發生的。只要特定位置的位置信息(包括其偏移量)聚集在一起,位置的順序就不會讓我感到困擾。任何想法我可以做到這一點? –