0

Hey Guys im new at coding and working right now on a Twitch Viewer. (FreeCodeCamp)

Im able to get information from the JSON file and show it through my html code. But my problem is that i cant get the names from my "gamer" array.

Why is the for loop not working in the json function?

Thank you very much for your help!

var gamer = ["OgamingSC2","ESL_SC2"];

for(i=0;i<(2);i++){
  
$.getJSON('https://api.twitch.tv/kraken/streams/'+gamer[i]+'/?callback=?', function(json) {
  $('.list').append("<b>Name: " + gamer[i] + "</b><br>");
  var logo = json.stream.channel.logo;
  
  $('.list').append("Game: " + json.stream.channel.game + "<br>");
  $('.list').append("Status: " + json.stream.channel.status + "<br>");
  $('.list').append("Logo: " + "<img src=" + logo + ">" + "<br><br><br>");
  console.log(json);
});}
img {
  width: 5em;
  border-radius: 10px;
}
<head><script src="http://code.jquery.com/jquery-1.11.2.min.js"></script> </head>
<body>
  <b>Twitch.tv JSON API</b> </br>
<div class="list"></div>
</body>
aisen
  • 1
  • 2

3 Answers3

0

What happens here is the $.getJson is an asynchronous call, which means that it will execute only after all of the sync operations are executed. So, the for loop runs twice, and two getJSON callback functions are added to the function stack.

Now as Javascript has lexical or functional scope, the variable i lives on in the global scope as 2 (as the final i++ also has executed).

Next, the callback functions are executed one by one off of the function stack. But alas, your variable i is now 2! Now the callback functions would only see gamer[2] which doesn't exist and this throws an undefined

If you add a console.log(i) to your $.getJSON, you'll see that it outputs only 2 two times.

Why don't you try this:

    var gamer = ["OgamingSC2","ESL_SC2"];
    gamer.map(function(gamer) {
      loopIt(gamer);
    });

  function loopIt(gamer) {
    
 
 $.getJSON('https://api.twitch.tv/kraken/streams/'+gamer+'/?callback=?', function(json) {
      $('.list').append("<b>Name: " + gamer + "</b><br>");
      var logo = json.stream.channel.logo;
      
      $('.list').append("Game: " + json.stream.channel.game + "<br>");
      $('.list').append("Status: " + json.stream.channel.status + "<br>");
      $('.list').append("Logo: " + "<img src=" + logo + ">" + "<br><br><br>");
    });

}  
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head><script src="http://code.jquery.com/jquery-1.11.2.min.js"></script> </head>
<body>
  <b>Twitch.tv JSON API</b> </br>
<div class="list"></div>
</body>
nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • Thank you for your really deep explanation! Now i understand whats happening! Thank you very much!!! – aisen Aug 26 '16 at 05:14
0

i think it json gives nothing because you put callback=? when you pass ? in call back variable it take value with ? and gives nothing. so just put callback= .dont give any values and try again. and just alert json variable in function. you will know value is coming or not. i dont found nothing wrong with loop and json link.

Abhinav
  • 388
  • 4
  • 12
0

For this types of server call in for loop , you can use custom loop.

Like this, https://jsfiddle.net/s8eLhxpx/

    var gamer = ["OgamingSC2","ESL_SC2"];

     var initCounter = 0;
     callServerData()

     function callServerData(){
        $.getJSON('https://api.twitch.tv/kraken/streams/'+gamer[ initCounter ]+'/?callback=?', function(json) {
    $('.list').append("<b>Name: " + gamer[ initCounter ] + "</b><br>");
    var logo = json.stream.channel.logo;

     $('.list').append("Game: " + json.stream.channel.game + "<br>");
     $('.list').append("Status: " + json.stream.channel.status + "<br>");
     $('.list').append("Logo: " + "<img src=" + logo + ">" + "<br><br><br>");
      console.log(json);
      initCounter++
      if(initCounter < gamer.length){

        callServerData();
     }

});
}
Pratik Parekh
  • 447
  • 4
  • 19