Look at this Codepen: http://codepen.io/anon/pen/mPeqBQ
var streamers = ["WagamamaTV", "losty123", "BlackDotATV", "Dendi", "VITALIC_DOTA2", "Singsing", "zonixxcs", "Attackerdota", "purgegamers", "Mikerina","ESL_LOL","jcarverpoker", "gnumme"];
The code query looks as follows:
$.each(streamers, function(key, value) {
$.getJSON('https://api.twitch.tv/kraken/streams/' + value, function(channel) {
if (channel["stream"] == null) {
// ADD TABLE ROW WITH DATA (for details see codepen
} else {
// ADD TABLE ROW WITH DATA (for details see codepen)
}
});
});
The Order in which the table rows are added is more or less random. Can I influence this order somehow so that it preserves the original order given in the array "streamers"? Just go to the codepen and refresh it a few times, you'll see what I mean.
Thanks
whendonelike @KevinB suggested.getJSONis asynchronous. What's happening in your case is that you create a bunch of ajax requests in a loop, but they don't wait on each other, and they don't finish in the same order they were created. That's why you see this randomness.$.when(dothis,dothat)with$.getJSON()in my answer it would solve this problem. Initially when i revoked my binding dupe vote I did it because i wasn't sure at the time if it was in fact doing the same thing.