JS循环不循环,直到我注释掉我的开关方法

JS Loop not looping until I comment out my switch method

本文关键字:循环 我的 方法 注释 开关 JS      更新时间:2023-09-26

被困在一个问题上几个小时了,现在我的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,看看是否修复了它。即使没有,你也会更好。