I have the following variables ready to hold strings that are returned from the API call:
// Variables for the Twitch user's object
var tName = "tName";
var tLogo = "tLogo";
var tGame = "tGame";
var tChannel = "tChannel";
Then I have this function which holds an AJAX call:
function twitchInfo(user){
$.ajax({
url: streams + user,
success: function(response){
if (response.stream){
tName = response.stream.channel.display_name;
tLogo = response.stream.channel.logo;
tGame = response.stream.game;
tChannel = response.stream.channel.status;
} else {
$.ajax({
url: users + user,
success: function(data){
tName = data.display_name;
if (data.logo) {
tLogo = data.logo} else {tLogo = defLogo}
tGame = "Offline";
tChannel = " ";
}
})
};
}
})
};
The function is being called from a loop that iterates through an array of users.
I checked the call URL's and they all return data just fine.
I wanted that data from the ajax call(s) to update the variables, but on investigating by doing a console.log(tName + tLogo ....), nothing is being updated.
Can anyone spot why? Any thoughts would be appreciated.
Thanks
edit
$(document).ready(function() {
//the Twitch accounts to include:
var twitchUsers = ["OgamingSC2", "ESL_SC2", "FreeCodeCamp", "storbeck", "brunofin", "comster404", "lastUser"];
var defLogo = "https://cdn1.iconfinder.com/data/icons/user-experience/512/user-unknown-512.png";
//Beginning of API call
var streams = "https://api.twitch.tv/kraken/streams/";
var users = "https://api.twitch.tv/kraken/users/";
//Twitch user's object which will hold the info from the API calls.
var AccInfo= {};
// Variables for the Twitch user's object
var tName = "tName";
var tLogo = "tLogo";
var tGame = "tGame";
var tChannel = "tChannel";
//Object constructor
function twitchUser(name, logo, game, channel){
this.name = name;
this.logo = logo;
this.game = game;
this.channel = channel;
}
function twitchInfo(user){
$.ajax({
url: streams + user,
success: function(response){
if (response.stream){
tName = response.stream.channel.display_name;
tLogo = response.stream.channel.logo;
tGame = response.stream.game;
tChannel = response.stream.channel.status;
} else {
$.ajax({
url: users + user,
success: function(data){
tName = data.display_name;
if (data.logo) {
tLogo = data.logo} else {tLogo = defLogo}
tGame = "Offline";
tChannel = " ";
}
})
};
}
})
};
for (p=0; p<twitchUsers.length; p++){
twitchInfo(twitchUsers[p]);
$("#theTable").append("<tr><td class=\"theLogo\"><img src=" + AccInfo.logo + "></td><td class=\"user\"><a href=\"http://www.twitch.tv/" + AccInfo.name + "\">"+ AccInfo.name +"</td><td>"+ AccInfo.game + " " + AccInfo.channel + "</td></tr>");
console.log(twitchUsers[p] + " " + tName + " " + tLogo + " " + tGame + " " + tChannel + " ");
}
});