Hello, I have been searching everywhere, but have not figured this out. Basically, I have been trying to update a global variable from within my ajax call. My ajax call is trying to update an array I have created of both online and offline streamers when going through an if-else statement to check for which streamer is online. The problem is that my global variables are returning in the console “TypeError: offline/online is undefined”. Perhaps a snippet of my code can clarify a bit better:
$(document).ready(function() {
var online, offline;
var streamers = ["ninja", "freecodecamp", "summit1g", "TimTheTatman", "CDNThe3rd"];
$('#online').click(function() {
setOnlineOffline();
console.log(online);
});
$('#offline').click(function() {
setOnlineOffline();
console.log(offline);
});
function setOnlineOffline() {
for (var i = 0; i < streamers.length; i++){
url = 'https://wind-bow.gomix.me/twitch-api/streams/' + streamers[i];
$.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
success: function(data) {
if (data.stream !== null){
online.push(streamers[i]);
}
else {
offline.push(streamers[i]);
}
},
error: function(errorMessage){
alert("Error")
}
});
};
}
});