Jquery For Loop结束代码

时间:2016-07-06 22:07:05

标签: jquery api loops for-loop ujs

我有2个for循环运行一个接一个。两个循环几乎用不同的信息做同样的事情,但问题是我首先运行的循环,以及循环后的所有内容,无论什么代码都不会运行。两个循环都可以完美地工作,但是所有这些都取决于哪一个在代码行中更早,它在第一个循环之后也不会执行任何操作。谁能告诉我为什么?这是我的代码

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 + ">")
              }
            });
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您需要为循环被视为完成的时间设置条件。

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

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

否则它们将永远运行(尝试在第一个循环中使用console.log(“test”),你会看到它无限打印)。

有关详细信息,请参阅this