JS循环不循环,直到我注释掉我的开关方法
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])被注释掉,循环就会工作,但显然它不会调用函数。
你知道为什么会这样吗?
我将冒险猜测,您在for
语句中使用的全局变量i
正在被calculateHMC()
击败,导致您的for
循环过早终止。
无论如何,将for
语句更改为使用局部变量而不是隐式全局变量:
for (var i = 0; i < 10; i++)
检查calculateHMC()
是否有类似的问题。即使这不是你代码中唯一的问题,它也是一个应该修复的问题。
事实上,如果你在严格模式下运行你的代码,它会指出所有这些错误给你,当然,这是使用严格模式的原因之一,这样你就不会意外地犯这些错误。
我不得不注意到i
没有被声明为局部变量,因此是一个全局变量。可能calculateHmc()
也有同样的错误,并且正在将i
的值向前推进超过9。
在两个函数中声明var i
,看看是否修复了它。即使没有,你也会更好。
相关文章:
- 我的javascript for循环不起作用
- 我没有'I don’我不理解for在循环中的用途
- 为什么我的While循环不起作用
- JS-我的循环使用setTimeout停止循环
- 我的Javascript for循环在1次迭代后停止
- 为什么这会造成一个无休止的循环并使我的浏览器崩溃
- 我的复选框没有't使用的循环来显示数据
- 为什么这个 fs.readFile 循环没有将其结果推送到我的数组
- 为什么我的“for”循环只获取 html 中的最后一项
- 我想让我的程序重新运行开头,有点像循环 JavaScript
- 嵌套在WHILE循环中的IF语句的结果混淆了我(Javascript)
- addEventListener() 到我的类与循环
- 将循环和数组合并到我的代码中
- 让循环在我的脚本中工作
- 我的单选按钮没有从foreach循环中获得值,表单验证返回'必填字段'即使已检查
- 当我'm试图覆盖循环中的元素
- Javascript:connection.query函数正在破坏我的for循环
- 我如何不在 javascript 中无限循环我的程序
- 如何循环我的代码
- 用javascript循环我的按钮点击