【发布时间】:2018-03-22 13:16:06
【问题描述】:
for 计数器中的 i 变量在为每个 Twitch 流媒体获取每个 API 时有效,但是当我使用它生成 div 时,它只显示为 8。是在获取 API 数据并遍历流媒体时,有什么方法可以使计数器工作?
$(document).ready(function(){
// streamers I want to look up
var streamers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]
// counts the number of streamers in the array
for (var i = 0; i < streamers.length; i++){
// gets each streamer, one by one
$.getJSON("https://wind-bow.gomix.me/twitch-api/streams/" + streamers[i] +"?callback=?", function(json) {
//if they are not offline, pulls information from them and adds it to a div
if ((json.stream) !== null) {
$("#results").prepend("<div class = \"resultsONLINE\">" + json.stream.channel.display_name + "</div>");
// if they are offline, marks them as offline
} else {
$("#results").append("<div class = \"resultsOFFLINE\">" + streamers[i] + " is offline</div>");
}
});
};
【问题讨论】:
标签: javascript api for-loop if-statement