我試圖循環播放一組隊列以獲取播放器數據並將數據傳遞給對象。我的ajax調用獲取文件。在通過所有團隊循環之後,我想用收集的數據編寫日誌。問題是,writeLog()立即調用 - 我認爲 - 不等待循環完成。這是一個懸掛問題嗎?回調問題?如何在for循環完成後重構我的代碼以寫入Log()?吊裝或回調問題?在writeLog()之前完成(循環),但在完成之前完成日誌寫入
(function(){
$('#generate-report').on('click', function(){
var logObj = {
playerCount: 0,
firstNameArray: [],
lastNameArray: [],
firstNameCharCount: 0,
lastNameCharCount: 0,
resultFirst: 0,
resultLast: 0,
freqReportFirst: "First name\n",
freqReportLast: "Last name\n",
freqObjFirst: {},
freqObjLast: {},
}
var url = "http://feeds.nfl.com/feeds-rs/roster/";
var teams = new Array("3800", "0200", "0325", "0610", "0750", "0810", "0920", "1050",
"1200", "1400", "1540", "1800", "2120", "2200", "2250", "2310",
"2700", "3000", "3200", "3300", "3410", "3430", "2520", "3700",
"3900", "4400", "4600", "4500", "2510", "4900", "2100", "5110"
);
//var teams = new Array("3800"); // For testing
for(var i=0; i<teams.length; i++){
console.log(teams[i]);
$.ajax({
url: url + teams[i] + ".json",
type: 'GET',
success: function(response){
processPlayerNames(response, logObj);
},
error: function(response){
console.log(response);
}
});
};
writeLog(logObj);
});
})();
它不是一個提升的問題,它不是一個回調的問題,這是一個不知道如何處理異步方法問題 –
@sbaden'WRITELOG(logObj)'可以被之前的所有名爲'$阿賈克斯( )'電話完成。 – guest271314