I am very new to programming, I am doing freecodecamp twitch tv JSON API project, I managed to get the names in different divs but everytime I refresh it has a different order (I want order to be same every time), I tried everything can someone please explain what I need to do???
function twitchTV(){
var twitchers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
var output = "";
for (var i = 0; i < twitchers.length; i++){
var channelName = twitchers[i];
var apiQueryBio = 'https://wind-bow.glitch.me/twitch-api/users/' + channelName + '/';
$.getJSON(apiQueryBio, function(json) {
output += '<div class="name">' + json["name"] + '</div>';
$("#name").html(output);
});
}
}
twitchTV();
body {
margin-top: 30px;
}
.name {
border: 2px solid black;
margin: 5px;
padding: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div id="name"></div>
<div id="bio"></div>
<div id="stream"></div>
</div>
forEach()won't make any difference. The results come back in seemingly random order because$.getJSONmakes asynchronous calls.$.getJSON. This function does a network request which will take some time. Some may take 5ms, others can take 20. And they can switch depending on internet speed. It's like telling 20 people to go get information. While they're getting it, one can run into traffic, another can trip over a rock, and another can have a clear route and finish fast. So you'll have to get creative on how you want to handle the data once it comes back from the internet.