Hi fellow campers, so I am here once again reaching out for you advice. More precisely, explanation.
I figured the problem out myself but I don’t know why it works the way it works and I’d like to know.
So here’s the working code:
var streamers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas", "sodapoppin"];
var xhttps = new Array();
var xhttps_responses = new Array();
for(var x = 0; x < streamers.length; x++){
xhttps[x] = new XMLHttpRequest();
console.log(xhttps[x]);
xhttps[x].open('GET', "https://wind-bow.glitch.me/twitch-api/streams/" + streamers[x]);
xhttps[x].onreadystatechange = readyStateHandler(xhttps[x]);
xhttps[x].send();
}
function readyStateHandler(xhttp){
return function(){
if(xhttp.readyState == 4){
console.log(xhttp.responseText);
}
}
}
And here is the code that doesn’t work:
var streamers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas", "sodapoppin"];
var xhttps = new Array();
var xhttps_responses = new Array();
for(var x = 0; x < streamers.length; x++){
xhttps[x] = new XMLHttpRequest();
console.log(xhttps[x]);
xhttps[x].open('GET', "https://wind-bow.glitch.me/twitch-api/streams/" + streamers[x]);
xhttps[x].onreadystatechange = readyStateHandler(xhttps[x]);
xhttps[x].send();
}
function readyStateHandler(xhttp){
if(xhttp.readyState == 4){
console.log(xhttp.responseText);
}
}
The only difference is that the onreadystatechange callback returns a function or executes a function.
Where’s the difference?
Thank you for explanation of any kind!