【发布时间】:2018-05-23 02:04:18
【问题描述】:
var url1="https://wind-bow.glitch.me/twitch-api/streams/";
var channelList= ["channel1","channel2",channel3"];
//I am trying to create for loop to define url with different channel names.
for(var i=0;i<channelList.length;i++){
var url="https://wind-bow.glitch.me/twitch-api/channels/" + channelList[i];
$.getJSON(url1+channelList[i],function(data){
//When the user click off button,I want to show offline channels.
$("#off").on("click", () => {
if(data.stream === null) {
$.getJSON(url,function(seconddata){
$("#main").append("<div class='row' style='margin-top:5px;'><div class='col-md-4'><p><img src='"+seconddata.logo+"'width='42' height='42'>"+" "+"<a href='"+seconddata.url+"' target=_blank>"+seconddata.display_name+"</a></p></div></div>")
});
}
});
});
你好,我对编码真的很陌生。现在我关注 FreeCodeCamp 并且我在 Twitch 查看器项目中苦苦挣扎。但我被困在代码上:) 我想创建一个 for 循环并使用 api 来获取所有通道的数据。但我只得到最后一个频道的数据,因为“i”在 jquery 函数之外增加了。它没有通过 jquery 函数。我该如何解决? :)
提前感谢您的回复。
注意:对不起我的英语。这不是我的母语。 :) 希望你能理解我的问题。
【问题讨论】:
-
这可能只是一个复制/粘贴错误,但是您在
channel3开头的channelList数组中缺少引号 -
是的,我在更改频道名称时忘记了。谢谢你:)
标签: javascript jquery for-loop getjson