2016-07-06 1 views
0

2 개의 for 루프가 하나씩 실행됩니다. 두 루프는 서로 다른 정보로 거의 같은 일을하지만, 문제는 루프를 실행 한 후 첫 번째 실행을 넣고 코드가 실행되지 않아도 루프를 끝내야한다는 것입니다. 두 루프 모두 완벽하게 작동하지만 코드 줄의 어느쪽에 더 가느냐에 따라 다르지만 첫 번째 루프 이후에는 아무 것도 실행하지 않습니다. 아무도 그 이유를 말할 수 있습니까? 여기 내 코드Jquery For Loop 종료 코드

for (i = 0; purplerescount.length; i++) { 
    if (pbigone[i].length > 0) { 
     $.ajax({ 
      url: "https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion/" + purplerescount[i], 
      method: "GET", 
      data:{ 
      api_key: "60304d9a-bf05-49ce-bd2a-30e6cc3f3863" 
      }, 
      success: function(response) { 
      var results = response.key 
      var square = "http://ddragon.leagueoflegends.com/cdn/6.12.1/img/champion/" + results + ".png" 
      $('#purpleresults').append("<img id='counterpics' src=" + square + ">") 
      } 
     }); 
     pbigone[i].forEach(function(entry){ 
      $.ajax({ 
       url: "https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion/" + entry, 
       method: "GET", 
       data:{ 
       api_key: "60304d9a-bf05-49ce-bd2a-30e6cc3f3863" 
       }, 
       success: function(response) { 
       var results = response.key 
       var square = "http://ddragon.leagueoflegends.com/cdn/6.12.1/img/champion/" + results + ".png" 
       $('#purpleresults').append("<img id='counterpics' src=" + square + ">") 
       } 
      }); 
     }); 
    } 
} 

for (x = 0; bluerescount.length; x++) { 
    if (bbigone[x].length > 0) { 
     $.ajax({ 
      url: "https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion/" + bluerescount[x], 
      method: "GET", 
      data:{ 
      api_key: "5a3cb583-47f0-4344-89ab-6c52b15f4082" 
      }, 
      success: function(response) { 
      var results = response.key 
      var square = "http://ddragon.leagueoflegends.com/cdn/6.12.1/img/champion/" + results + ".png" 
      $('#blueresults').append("<img id='counterpics' src=" + square + ">") 
      } 
     }); 
     bbigone[x].forEach(function(entry){ 
      $.ajax({ 
       url: "https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion/" + entry, 
       method: "GET", 
       data:{ 
       api_key: "5a3cb583-47f0-4344-89ab-6c52b15f4082" 
       }, 
       success: function(response) { 
       var results = response.key 
       var square = "http://ddragon.leagueoflegends.com/cdn/6.12.1/img/champion/" + results + ".png" 
       $('#blueresults').append("<img id='counterpics' src=" + square + ">") 
       } 
      }); 
     }); 
    } 
} 

답변

0

루프가 완료된 것으로 간주되는 조건을 설정해야합니다.

for (i = 0; purplerescount.length < i; i++) { ... } 

다른
for (x = 0; bluerescount.length < x; x++) { ... } 

가 (첫 번째 루프 내을 console.log ("테스트"를 사용해보십시오) 당신이 그것을 무한히 인쇄 볼 수 있습니다) 영원히 실행하겠습니다.

자세한 내용은 this을 참조하십시오.

+1

조건이 거꾸로 됨 – charlietfl