Hi, I have been working on implementing a for loop for my Twitch Streamers app so that I can allow users to add streamers they like into the array with the streamers’ names and have those streamers automatically displayed.
To do this, I put strings (the streamers’ names) in an array through two different ajax calls. Divs are then created with ids of the name of string and include data about what twitch streamers are streaming from the ajax requests.
I was struggling to make it so that the element/streamer name would go through the first ajax call and then goes through the second because ajax calls are asynchronous. As a result, elements in the array would go through the second ajax request before the first one was completed and try to go into divs that were not yet created.
To try to fix this, I put the second ajax request within the first one like so:
ajax request{
code that makes divs
second ajax request}
This seems to have made the elements in the array come back in the correct order, but the divs were still not displaying the data from the ajax requests correctly. I noticed that when logging the name variable, that only one element in the array seemed to get passed through to the second ajax request.
Below is the old JS:
$(document).ready(function() {
var twichUrl = "https://wind-bow.glitch.me/twitch-api";
var channels = ["ESL_SC2", "freecodecamp", "OgamingSC2"];
var name = "";
var name2 = "";
var logo = "";
for (i = 0; i < channels.length; i++) {
twitchUrl =
"https://wind-bow.glitch.me/twitch-api/channels/" +
channels[i] +
"?callback=?";
twitchUrlStream =
"https://wind-bow.glitch.me/twitch-api/streams/" +
channels[i] +
"?callback=?";
$.ajax({
type: "GET",
url: twitchUrl,
dataType: "jsonp",
jsonp: "callback",
success: function(data) {
name = data.display_name;
logo = data.logo;
console.log('channel data below:')
console.log(name);
$("body").append(
"<div id=" +
name +
"Container>" +
' <span class ="icon" id =' +
name +
"><img src=" +
data.logo +
">" +
"</span>" +
'<span class = "status" id = "' +
name +
'data"></span>' +
"</div>"
);
$.ajax({
type: "GET",
url: twitchUrlStream,
dataType: "jsonp",
jsonp: "callback",
success: function(data) {
console.log('stream data below:')
console.log(name);
if (data.stream === null) {
$("#" + name + "data").html(
" " +
'<a target = "_blank" href ="https://www.twitch.tv/' +
name +
'">' +
name +
"</a>" +
" is currently offline"
);
} else {
$("#" + name + "data").html(
" " +
'<a target = "_blank" href ="https://www.twitch.tv/' +
data.stream.channel.display_name +
'">' +
data.stream.channel.display_name +
"</a>" +
" is Streaming: " +
data.stream.game +
"-- " +
data.stream.channel.status
);
}
}
});
}
});
}
});
However, I got some help and made two changes:
- I defined the ‘twitchUrlStream’, ‘twitchUrl’, ‘logo’, and ‘name’ variables with “let” rather than var.
- I defined ‘logo’ and ‘name’ within the for loop.
Here is the codepen now: https://codepen.io/mso122591/pen/jxNoBV
The problem is that I don’t understand much about var vs let and why this change seems to have fixed everything. The person said that using let creates a local variable, but more details/explanation would be great. Was var vs let truly the problem? Could I have solved this by keeping this variables defined as var?