提升还是回调问题?在 writeLog() 之前完成 for(loop) 但在完成之前写入日志
Hoisting or Callback issue? Finish for(loop) before writeLog() but log writes before done
我正在尝试遍历一组团队以获取玩家数据并将数据传递给一个对象。我的 ajax 调用获取文件。在遍历所有团队之后,我想用收集到的数据写一个日志。问题是 writeLog() 被立即调用 - 我认为 - 而不是等待循环完成。这是吊装问题吗?回调问题?在 for 循环完成后,如何将我的代码重构为 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);
});
})();
$.ajax()
returns 异步结果。您可以将 $.ajax()
调用推送到一个数组,然后在 promise 对象数组上使用 Promise.all()
、.map()
,或者使用 $.when()
、Function.prototype.apply()
、.map()
在 .then()
处调用 writeLog()
之前执行所有异步任务链接到表示从 $.when()
调用返回的 jQuery promise 对象的变量。
var requests = $.when.apply(null, teams.map(function(team) {
return $.ajax({
url: url + team + ".json",
type: 'GET',
success: function(response){
processPlayerNames(response, logObj);
}
});
}));
requests.then(function() {
writeLog(logObj)
}, function(response){
console.log(response);
});
我正在尝试遍历一组团队以获取玩家数据并将数据传递给一个对象。我的 ajax 调用获取文件。在遍历所有团队之后,我想用收集到的数据写一个日志。问题是 writeLog() 被立即调用 - 我认为 - 而不是等待循环完成。这是吊装问题吗?回调问题?在 for 循环完成后,如何将我的代码重构为 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);
});
})();
$.ajax()
returns 异步结果。您可以将 $.ajax()
调用推送到一个数组,然后在 promise 对象数组上使用 Promise.all()
、.map()
,或者使用 $.when()
、Function.prototype.apply()
、.map()
在 .then()
处调用 writeLog()
之前执行所有异步任务链接到表示从 $.when()
调用返回的 jQuery promise 对象的变量。
var requests = $.when.apply(null, teams.map(function(team) {
return $.ajax({
url: url + team + ".json",
type: 'GET',
success: function(response){
processPlayerNames(response, logObj);
}
});
}));
requests.then(function() {
writeLog(logObj)
}, function(response){
console.log(response);
});