-1

I have two functions both of which makes a jQuery call , the first one gets the champion ID and the second one gets the champion name using the ID, now I have 10 champion names to get so I'm using a for loop but it just won't work ...
This is my code :

function GetChampName(id) {
  $.ajax({
    url: 'https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/' + id + '?api_key=f8453a98-ad2c-455e-b9df-f46c4f99d0ed',
    datatype: 'json',
    type: 'get',
    success: function(json) {
      sName = json.name;
    }
  });
  return sName;
}

function summonerLookUp() {
  var cn = [];
  $.ajax({
    url: 'https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/46743451/recent?api_key=f8453a98-ad2c-455e-b9df-f46c4f99d0ed',
    datatype: 'json',
    type: 'get',
    success: function(json) {
      for (var i = 0; i <= json.games.length; i++) {
        var id = json.games[i].championId;
        $('#t1 tr:last').after('<tr> <td>' + json.games[i].gameId + '</td><td id="e">' + cn[i] + '</td><td>' + wl(json.games[i].stats.win) + '</td> </tr>');
      }
    }
  });
}
Just Away
  • 9
  • 6

1 Answers1

-1
function GetChampName(id,cb) {
  $.ajax({
    url: 'https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/' + id + '?api_key=f8453a98-ad2c-455e-b9df-f46c4f99d0ed',
    datatype: 'json',
    type: 'get',
    success: function(json) {
      sName = json.name;
      cb(sName)
    }
  });
}

function summonerLookUp() {
  $.ajax({
    url: 'https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/46743451/recent?api_key=f8453a98-ad2c-455e-b9df-f46c4f99d0ed',
    datatype: 'json',
    type: 'get',
    success: function(json) {
      for (var i = 0; i <= json.games.length; i++) {
        if(json.games[i]){
          var id = json.games[i].championId;
          GetChampName(id,function(sName){
            console.log(sName);
            var html = '<tr> <td>';
            html += json.games[i].gameId;
            html += '</td><td id="e">' + sName + '</td><td>';
            html += wl(json.games[i].stats.win);
            html += '</td> </tr>';
            $('#t1 tr:last').after(html);
          });
        }
      }
    }
  });
}

summonerLookUp();
Eugene Hauptmann
  • 1,255
  • 12
  • 16