I have a problem with my code. It does not run in order, making a getJSON
request
takes a bit.
$(document).ready(function(){
var channels = ['ESL_SC2', 'OgamingSC2', 'cretetion', 'freecodecamp', 'storbeck', 'habathcx', 'RobotCaleb', 'noobs2ninjas'];
getChannelsNames();
function getChannelsNames(){
var myHtml = '', status = 'Offline';
channels.forEach(function(channel, callback){
$.getJSON('https://wind-bow.glitch.me/twitch-api/streams/' + channel, function(json){
status = onlineStatus(json);
myHtml += infoChannels(channel, status);
}).fail(function(){
console.log('Error');
myHtml += infoChannels(channel, status);
});
});
$('.channels').html(myHtml);
}
function onlineStatus(json){
if(json.stream){
return json.stream.game;
} else {
return 'Offline';
}
}
function infoChannels(channel, status){
return "<div class='row'><div class='col-12 col-md-4 bg-info'>" + channel + "</div><div class='col-12 col-md-8 bg-primary text-center'>" + status + "</div></div>";
}
});
In this part $('.channels').html(myHtml);
does not do what it should do because the variable myHtml
would have to contain something that should be assigned inside getJSON
.