JS Loop 在我注释掉我的 switch 方法之前不会循环
JS Loop not looping until I comment out my switch method
我的 for 循环不会循环(只进行 1 次迭代)的问题已经被困了几个小时。
以下是我的代码:
function getScore(summonerId) {
$.ajax({
dataType: "json",
type: 'GET',
url: 'https://na.api.pvp.net/api/lol/na/v2.2/matchhistory/' + summonerId + '/?queueType=NORMAL_5x5_BLIND,NORMAL_5x5_DRAFT,RANKED_SOLO_5x5,RANKED_PREMADE_5x5,NORMAL_5x5_DRAFT,RANKED_TEAM_5x5&beginIndex=0&endIndex=9&api_key=***',
success: function (data, state) {
for (i = 0; i < 10; i++){ // THIS IS THE LOOP THAT WON'T WORK.
console.log(i);
console.log(state);
//v1.1.0
if (data.matches[i].participants[0].stats.firstBloodKill == true){
firstBloodArray[i] = 1;
}else{
firstBloodArray[i] = 0;
}
if (data.matches[i].participants[0].stats.firstTowerKill == true){
firstTowerArray[i] = 1;
}else{
firstTowerArray[i] = 0;
}
if (data.matches[i].participants[0].stats.firstTowerKill == true){
firstInhibitorArray[i] = 1;
}else{
firstInhibitorArray[i] = 0;
}
damageDealtArray[i] = data.matches[i].participants[0].stats.totalDamageDealt;
healingDealtArray[i] = data.matches[i].participants[0].stats.totalHeal;
damageTakenArray[i] = data.matches[i].participants[0].stats.totalDamageTaken;
inhibitorArray[i] = data.matches[i].participants[0].stats.inhibitorKills;
towerArray[i] = data.matches[i].participants[0].stats.towerKills;
//timeline arrays
tl_arr_cspm_zero_ten[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.zeroToTen;
tl_arr_cspm_ten_twenty[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.tenToTwenty;
if(data.matches[i].participants[0].timeline.creepsPerMinDeltas.twentyToThirty != undefined) {
tl_arr_cspm_twenty_thirty[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.twentyToThirty;
}else{
tl_arr_cspm_twenty_thirty[i] = 0
}
if(data.matches[i].participants[0].timeline.creepsPerMinDeltas.thirtyToEnd != undefined) {
tl_arr_cspm_thirty_end[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.thirtyToEnd;
}else{
tl_arr_cspm_thirty_end[i] = 0
}
tl_arr_xppm_zero_ten[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.zeroToTen;
tl_arr_xppm_ten_twenty[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.tenToTwenty;
if(data.matches[i].participants[0].timeline.xpPerMinDeltas.twentyToThirty != undefined) {
tl_arr_xppm_twenty_thirty[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.twentyToThirty;
}else{
tl_arr_xppm_twenty_thirty[i] = 0;
}
if(data.matches[i].participants[0].timeline.xpPerMinDeltas.thirtyToEnd != undefined){
tl_arr_xppm_thirty_end[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.thirtyToEnd;
}else{
tl_arr_xppm_thirty_end[i] = 0;
}
tl_arr_gpm_zero_ten[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.zeroToTen;
tl_arr_gpm_ten_twenty[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.tenToTwenty;
if(data.matches[i].participants[0].timeline.goldPerMinDeltas.twentyToThirty != undefined){
tl_arr_gpm_twenty_thirty[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.twentyToThirty;
}else{
tl_arr_gpm_twenty_thirty[i] = 0;
}
if(data.matches[i].participants[0].timeline.goldPerMinDeltas.thirtyToEnd != undefined){
tl_arr_gpm_thirty_end[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.thirtyToEnd;
}else{
tl_arr_gpm_thirty_end[i] = 0;
}
//v1.0.2
csArray[i] = data.matches[i].participants[0].stats.minionsKilled;
assistArray[i] = data.matches[i].participants[0].stats.assists;
killArray[i] = data.matches[i].participants[0].stats.kills;
gameTimeArray[i] = data.matches[i].matchDuration;
championArray[i] = data.matches[i].participants[0].championId;
deathArray[i] = data.matches[i].participants[0].stats.deaths;
jungleCSArray[i] = data.matches[i].participants[0].stats.neutralMinionsKilledTeamJungle;
counterJungleCSArray[i] = data.matches[i].participants[0].stats.neutralMinionsKilledEnemyJungle;
roleArray[i] = data.matches[i].participants[0].timeline.role;
laneArray[i] = data.matches[i].participants[0].timeline.lane;
crowdControlArray[i] = data.matches[i].participants[0].stats.totalTimeCrowdControlDealt;
switch (roleArray[i]) {
case "DUO":
case "SOLO":
//assumes top lane duo player or solo mid or solo top
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 5, 0.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 3, 1.5, 1, 0.9, 0.8, 0.7, 1, 1.1, 1.1, 1.1, 1, 1.1, 1.2, 1.3, i);
break;
case "DUO_CARRY":
//assumes bot lane adc
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 5, 0.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 3, 2, 1, 0.9, 0.8, 0.7, 0.8, 0.8, 0.8, 0.8, 1, 1.1, 1.2, 1.3, i);
break;
case "DUO_SUPPORT":
//assumes bot lane support
calculateHmc(1.33, 1, 0.75, 0.06, 0.11, 0.17, 0.5, 5, 4, 0.5, 0.16, 0.65, 2, 1.25, 0.25, 3.5, 0.1, 0.1, 0.1, 0.1, 0.8, 0.8, 0.8, 0.8, 0.3, 0.4, 0.5, 0.6, i);
break;
case "NONE":
//assumes jungle, check lane
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 2, 2.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 1.5, 1.5, 0.6, 0.5, 0.4, 0.3, 1, 0.9, 0.8, 0.7, 1, 0.9, 0.8, 0.7, i);
break;
}
}
}
});
如果我将开关注释掉,循环就会工作。
我曾尝试重写循环以确保它不是我的循环声明中的语法错误,但事实并非如此。
我需要循环 10 次并调用 calculateHmc 函数 10 次,但它只执行了一次。
只要 switch (roleArray[i]) 被注释掉,循环就可以运行,但显然它不会调用函数。
知道为什么会发生这种情况吗?
我不禁注意到 i
没有声明为局部变量,因此是一个全局变量。也许 calculateHmc()
有同样的错误并且正在将 i
的值提高到 9.
在两个函数中声明 var i
,看看是否可以解决问题。即使没有,你也过得更好。
我冒昧地猜测,您在 for
语句中使用的全局变量 i
被 calculateHMC()
击败,导致您的 for
循环.
在任何情况下,更改 for
语句以使用局部变量而不是隐式全局变量:
for (var i = 0; i < 10; i++)
并且,检查 calculateHMC()
是否存在类似问题。即使这不是您代码中的唯一问题,也是应该解决的问题。
事实上,如果您 运行 您在 strict mode 中的代码,它会向您指出所有这些错误,这当然是使用严格模式的原因之一所以你不会不小心犯这些错误。
我的 for 循环不会循环(只进行 1 次迭代)的问题已经被困了几个小时。
以下是我的代码:
function getScore(summonerId) {
$.ajax({
dataType: "json",
type: 'GET',
url: 'https://na.api.pvp.net/api/lol/na/v2.2/matchhistory/' + summonerId + '/?queueType=NORMAL_5x5_BLIND,NORMAL_5x5_DRAFT,RANKED_SOLO_5x5,RANKED_PREMADE_5x5,NORMAL_5x5_DRAFT,RANKED_TEAM_5x5&beginIndex=0&endIndex=9&api_key=***',
success: function (data, state) {
for (i = 0; i < 10; i++){ // THIS IS THE LOOP THAT WON'T WORK.
console.log(i);
console.log(state);
//v1.1.0
if (data.matches[i].participants[0].stats.firstBloodKill == true){
firstBloodArray[i] = 1;
}else{
firstBloodArray[i] = 0;
}
if (data.matches[i].participants[0].stats.firstTowerKill == true){
firstTowerArray[i] = 1;
}else{
firstTowerArray[i] = 0;
}
if (data.matches[i].participants[0].stats.firstTowerKill == true){
firstInhibitorArray[i] = 1;
}else{
firstInhibitorArray[i] = 0;
}
damageDealtArray[i] = data.matches[i].participants[0].stats.totalDamageDealt;
healingDealtArray[i] = data.matches[i].participants[0].stats.totalHeal;
damageTakenArray[i] = data.matches[i].participants[0].stats.totalDamageTaken;
inhibitorArray[i] = data.matches[i].participants[0].stats.inhibitorKills;
towerArray[i] = data.matches[i].participants[0].stats.towerKills;
//timeline arrays
tl_arr_cspm_zero_ten[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.zeroToTen;
tl_arr_cspm_ten_twenty[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.tenToTwenty;
if(data.matches[i].participants[0].timeline.creepsPerMinDeltas.twentyToThirty != undefined) {
tl_arr_cspm_twenty_thirty[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.twentyToThirty;
}else{
tl_arr_cspm_twenty_thirty[i] = 0
}
if(data.matches[i].participants[0].timeline.creepsPerMinDeltas.thirtyToEnd != undefined) {
tl_arr_cspm_thirty_end[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.thirtyToEnd;
}else{
tl_arr_cspm_thirty_end[i] = 0
}
tl_arr_xppm_zero_ten[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.zeroToTen;
tl_arr_xppm_ten_twenty[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.tenToTwenty;
if(data.matches[i].participants[0].timeline.xpPerMinDeltas.twentyToThirty != undefined) {
tl_arr_xppm_twenty_thirty[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.twentyToThirty;
}else{
tl_arr_xppm_twenty_thirty[i] = 0;
}
if(data.matches[i].participants[0].timeline.xpPerMinDeltas.thirtyToEnd != undefined){
tl_arr_xppm_thirty_end[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.thirtyToEnd;
}else{
tl_arr_xppm_thirty_end[i] = 0;
}
tl_arr_gpm_zero_ten[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.zeroToTen;
tl_arr_gpm_ten_twenty[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.tenToTwenty;
if(data.matches[i].participants[0].timeline.goldPerMinDeltas.twentyToThirty != undefined){
tl_arr_gpm_twenty_thirty[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.twentyToThirty;
}else{
tl_arr_gpm_twenty_thirty[i] = 0;
}
if(data.matches[i].participants[0].timeline.goldPerMinDeltas.thirtyToEnd != undefined){
tl_arr_gpm_thirty_end[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.thirtyToEnd;
}else{
tl_arr_gpm_thirty_end[i] = 0;
}
//v1.0.2
csArray[i] = data.matches[i].participants[0].stats.minionsKilled;
assistArray[i] = data.matches[i].participants[0].stats.assists;
killArray[i] = data.matches[i].participants[0].stats.kills;
gameTimeArray[i] = data.matches[i].matchDuration;
championArray[i] = data.matches[i].participants[0].championId;
deathArray[i] = data.matches[i].participants[0].stats.deaths;
jungleCSArray[i] = data.matches[i].participants[0].stats.neutralMinionsKilledTeamJungle;
counterJungleCSArray[i] = data.matches[i].participants[0].stats.neutralMinionsKilledEnemyJungle;
roleArray[i] = data.matches[i].participants[0].timeline.role;
laneArray[i] = data.matches[i].participants[0].timeline.lane;
crowdControlArray[i] = data.matches[i].participants[0].stats.totalTimeCrowdControlDealt;
switch (roleArray[i]) {
case "DUO":
case "SOLO":
//assumes top lane duo player or solo mid or solo top
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 5, 0.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 3, 1.5, 1, 0.9, 0.8, 0.7, 1, 1.1, 1.1, 1.1, 1, 1.1, 1.2, 1.3, i);
break;
case "DUO_CARRY":
//assumes bot lane adc
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 5, 0.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 3, 2, 1, 0.9, 0.8, 0.7, 0.8, 0.8, 0.8, 0.8, 1, 1.1, 1.2, 1.3, i);
break;
case "DUO_SUPPORT":
//assumes bot lane support
calculateHmc(1.33, 1, 0.75, 0.06, 0.11, 0.17, 0.5, 5, 4, 0.5, 0.16, 0.65, 2, 1.25, 0.25, 3.5, 0.1, 0.1, 0.1, 0.1, 0.8, 0.8, 0.8, 0.8, 0.3, 0.4, 0.5, 0.6, i);
break;
case "NONE":
//assumes jungle, check lane
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 2, 2.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 1.5, 1.5, 0.6, 0.5, 0.4, 0.3, 1, 0.9, 0.8, 0.7, 1, 0.9, 0.8, 0.7, i);
break;
}
}
}
});
如果我将开关注释掉,循环就会工作。
我曾尝试重写循环以确保它不是我的循环声明中的语法错误,但事实并非如此。
我需要循环 10 次并调用 calculateHmc 函数 10 次,但它只执行了一次。
只要 switch (roleArray[i]) 被注释掉,循环就可以运行,但显然它不会调用函数。
知道为什么会发生这种情况吗?
我不禁注意到 i
没有声明为局部变量,因此是一个全局变量。也许 calculateHmc()
有同样的错误并且正在将 i
的值提高到 9.
在两个函数中声明 var i
,看看是否可以解决问题。即使没有,你也过得更好。
我冒昧地猜测,您在 for
语句中使用的全局变量 i
被 calculateHMC()
击败,导致您的 for
循环.
在任何情况下,更改 for
语句以使用局部变量而不是隐式全局变量:
for (var i = 0; i < 10; i++)
并且,检查 calculateHMC()
是否存在类似问题。即使这不是您代码中的唯一问题,也是应该解决的问题。
事实上,如果您 运行 您在 strict mode 中的代码,它会向您指出所有这些错误,这当然是使用严格模式的原因之一所以你不会不小心犯这些错误。