使用多个API调用进行循环-Javascript/Jquery
我有一组用户,我将通过他们循环获取他们的twitch信息。我有两个api调用:get_channel_info1和get_status_info1。get_channel_info1获取名称和徽标,并检查是否存在帐户。然后根据帐户状态,我想制作第二个api来查看它们是否在线。我创建了get_info来为一个用户进行顺序调用,但是现在我想在循环中使用这个方法 我猜循环没有等待get_info中的api调用完成,然后继续循环。我不知道如何克服这个问题。有人能帮我吗使用多个API调用进行循环-Javascript/Jquery,javascript,jquery,Javascript,Jquery,我有一组用户,我将通过他们循环获取他们的twitch信息。我有两个api调用:get_channel_info1和get_status_info1。get_channel_info1获取名称和徽标,并检查是否存在帐户。然后根据帐户状态,我想制作第二个api来查看它们是否在线。我创建了get_info来为一个用户进行顺序调用,但是现在我想在循环中使用这个方法 我猜循环没有等待get_info中的api调用完成,然后继续循环。我不知道如何克服这个问题。有人能帮我吗 function get_chan
function get_channel_info1(user) {
console.log(user);
return $.getJSON("https://" + user,function(data) {
if (data.status == null) {
twitch_channel.display_name = data.display_name;
twitch_channel.logo = data.logo;
twtich_channel.status = 200;
} else if (data.status == 404) {
twitch_channel.display_name = "User " + user + " doesn't exist";
twitch_channel.logo = "#";
twitch_channel.status = 404;
}
})
}
function get_status_info1(user) {
console.log("getting the status of " + user)
console.log(user)
return $.getJSON("https://" + user.display_name, function(data) {
if (data.stream != null) {
twitch_channel.status_title = data.stream.channel.status;
twitch_channel.status = "Online";
} else{
twitch_channel.status_title = "Null";
twitch_channel.status = "Offline";
}
})
}
function get_info(user) {
twitch_channel = {};
$.when(get_channel_info1(user)).then(function() { return get_status_info1(twitch_channel, user)} )
.then( function() { console.log(twitch_channel })
};
for (var i = 0; i < twitch_list.length; i++) {
console.log("in the for loop, calling " + twitch_list[i])
get_info(twitch_list[i]);
}
函数获取频道信息1(用户){
console.log(用户);
返回$.getJSON(“https://”+用户、函数(数据){
如果(data.status==null){
twitch_channel.display_name=data.display_name;
twitch_channel.logo=data.logo;
twtich_信道状态=200;
}else if(data.status==404){
twitch_channel.display_name=“User”+User+“不存在”;
twitch_channel.logo=“#”;
twitch_channel.status=404;
}
})
}
函数get_status_info1(用户){
log(“获取”+用户的状态)
console.log(用户)
返回$.getJSON(“https://”+user.display\u名称、函数(数据){
if(data.stream!=null){
twitch_channel.status_title=data.stream.channel.status;
twitch_channel.status=“在线”;
}否则{
twitch_channel.status_title=“Null”;
twitch_channel.status=“离线”;
}
})
}
函数获取信息(用户){
twitch_通道={};
$.when(get_channel_info1(用户)).then(函数(){return get_status_info1(twitch_channel,用户)})
.then(函数(){console.log(twitch_channel})
};
对于(变量i=0;i
我看到的一个问题是,您在获取状态信息1
中输入了两个参数,而您的函数只接受了一个参数。看起来您没有任何明显的异步问题,但您没有将用户
传递到函数中。请仔细阅读s和s。然后您可能会看到的答案。