Currently working on the Twitch.tv project and having a bit of problem with displaying the code.
Here is my code:
$(document).ready(function() {
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
for (i=0; i<users.length; i++) {
var url="https://api.twitch.tv/kraken/channels/" + users[i];
var logo;
var channel;
var status;
$.ajax ({
type: 'GET',
url: url,
headers: {
'Client-ID': 'hw8qejm816w6tjk88fdhfjz1wxpypm'
},
success: function(data) {
logo = data.logo;
channel = data.name;
}
});
var url2 = "https://api.twitch.tv/kraken/streams/" + users[i];
$.ajax ({
type: 'GET',
url: url2,
headers: {
'Client-ID': 'hw8qejm816w6tjk88fdhfjz1wxpypm'
},
success: function(data2) {
if (data2.stream == null) {
status = "Offline";
} else {
status = "Online";
};
}
});
$("#channelInfo").prepend("<div class='row'><div class='col-xs-4'><img src="+logo+"></div><div class='col-xs-4'>"+channel+"</div><div class='col-xs-4'>"+status+"</div></div>")
};
});
I am sure the API is working well. I make 2 API calls here, one to âhttps://api.twitch.tv/kraken/channels/â and another to âhttps://api.twitch.tv/kraken/streams/â, because I would like to display the channel logo even when a channel is offline, and the /streams/ calls doesnât display this information in the json when the stream is offline (or ânullâ).
Rest of my code can be found here: http://codepen.io/drhectapus/pen/VPNQJa
Ajax requests are asynchronous so the $("#channelInfo").prepend()
runs before any data is returned.
1 Like
How can I change the code so it works then?
You can put the two ajax requests in functions and use $.when
to check when they return data.
I forked your pen and implemented this: http://codepen.io/benjaminvanzwienen/pen/yMLpZG
1 Like
you need to âwait outâ for the ajax call to return anything, error or sucess. cause the way you have it set up the ajax calls will go up but the code will continue to process and it wonât wait for anything more. here this might shed some light on the issue and how to tackle it
Thanks for getting it to work. I kind of understand what youâve done here, but having trouble trying to understand it in detail, especially the âthenâ part.
$.when(request1(url), request2(url2)).then(function(data, data2)
I understand that when it executes both request1 and request2 functions, then it will execute function(data, data2).
But,
- How does it know that data is to be used by request1 and data2 is to be used by request2?
- Why does the data require index 0 now? E.g. data[0].logo as opposed to data.logo?
Also, Iâd like to add an error function to my AJAX calls for users which donât exist, such as âbrunofinâ and âcomster404â. How can I factor this code in?
Thanks it makes sense what youâre saying. I have another problem now though - I want to add in a way so that it executes something if thereâs an error with the AJAX call. For example, when I check to see if stream âbrunofinâ exists (which it doesnât). How can I factor this into the code? Is there a way to make it execute function if thereâs âerrorâ in AJAX call?
You can use the following syntax (http://api.jquery.com/jQuery.when/):
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
.then( myFunc, myFailure );
I changed my fork: http://codepen.io/benjaminvanzwienen/pen/yMLpZG, it console.log
the error message.