简体   繁体   English

Ajax请求加载时间太长

[英]Ajax request takes too long to load

I have a website about league of legends. 我有一个关于传说联盟的网站。 I request statistics from riot games API based on player's name. 我根据玩家的名字请求来自防暴游戏API的统计数据。 Then i get the information on json. 然后我得到关于json的信息。 Then i get the information from the php page to the main page with ajax. 然后我通过ajax从php页面获取信息到主页面。 But it really takes too long to load. 但加载真的需要很长时间。 I noticed that it reads one json registry every one second. 我注意到它每秒读取一个json注册表。

Here is my ajax code: 这是我的ajax代码:

function getStats (SUMMONER_ID, API_KEY) {
  var Topuser = SUMMONER_ID
  var theStatsDiv = document.getElementById('deaths')
  $.ajax({
    url: 'getKey.php',
    type: 'post',
    dataType: 'json',
    async: false,
    data: {urlLinked: 'https://' + regionSelected + '.api.pvp.net/api/lol/' + regionSelected + '/v1.3/stats/by-summoner/' + SUMMONER_ID + '/ranked?season=SEASON2016&api_key='},
    success: function (json) {
      var user = Topuser

      for (var i = 0; i < json.champions.length; i++) {
        if (json.champions[i].id != 0) {
          var wins = json.champions[i].stats.totalSessionsWon
          var loses = json.champions[i].stats.totalSessionsLost
          var $div = $('<div>', {id: 'champion' + i, class: 'championClass', 'percentage': wins + loses})
          $('#deaths').append($div)

          var ratio = 0

          if (wins == 0) {
            ratio = 0
          }
          if (loses == 0) {
            ratio = 100
          }
          if (wins != 0 && loses != 0) {
            ratio = (wins / (wins + loses)) * 100
            ratio = ratio.toFixed(0)
          }


          $.ajax({
            url: 'getKey.php',
            type: 'post',
            dataType: 'json',
            async: false,
            data: {urlLinked: 'https://global.api.pvp.net/api/lol/static-data/' + regionSelected + '/v1.2/champion/' + json.champions[i].id + '?api_key='},
            success: function (json) {
              championIcon = json.name
              var tempDif = 0
              tempDif = wins - loses
              if (tempDif > maxDifference) {
                difChampionName = ''
                maxDifference = 0
                maxDifference = tempDif
                maxRatio = ratio
                difChampionName = json.name
              }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
              var user = Topuser
              console.log(errorThrown)
              if (errorThrown === 'Not Found') {
              }
            }
          })

          var result = championIcon.replace(/[^A-Z0-9]+/ig, '')
          $('#champion' + i).append('<div class="championWrapper" id="championWrapper' + i + '">')
          $('#championWrapper' + i).append('<p class="championName">' + championIcon + '</p><br>')
          $('#champion' + i).css('background-image', 'url(http://ddragon.leagueoflegends.com/cdn/img/champion/splash/' + result + '_0.jpg)')


          if (ratio >= 50) {
            $('#championWrapper' + i).append("Stuff.....")
          }
          if (ratio < 50) {
            $('#championWrapper' + i).append("More stuff.....")
          }

        }
      }

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      var user = Topuser
      console.log(errorThrown)
      if (errorThrown === 'Not Found') {
        document.getElementById('deaths').innerHTML = 'not found'
      }
    }
  })

}

What can i do to make ajax calls run faster? 我怎样才能使ajax调用运行得更快? I also have another 2 functions but they only read one thing from the json array. 我还有另外两个函数,但它们只从json数组中读取一个东西。 Thank you so much for your time. 非常感谢您的参与。 Sorry for my bad English and sorry if the code looks bad. 抱歉我的英语不好,如果代码看起来很糟糕,我很抱歉。

(I don't yet have 50 rep, so I've added this as an answer instead.) (我还没有50个代表,所以我把它作为答案添加了。)

Is there any reason you're not grabbing: 你有没有理由不抓住:

/api/lol/static-data/{region}/v1.2/champion

and then sorting through all of the json data that you'll get in return? 然后对您将获得的所有json数据进行排序? That way you get all of the information you want in one ajax call, and you can run a loop to create your table, and make any calculations you'd like to make. 这样,您可以在一次ajax调用中获得所需的所有信息,并且可以运行循环来创建表,并进行任何您想要进行的计算。

By doing it individually for each champion: 通过为每个冠军单独做:

/api/lol/static-data/{region}/v1.2/champion/{id}

you're getting the same data (as far as I can tell) but you're getting it over multiple ajax calls, which is making things very slow for you. 你获得了相同的数据(据我所知)但是你通过多个ajax调用得到它,这对你来说非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM