JavaScript - 仅评估 if 语句的第一部分
JavaScript - Only the first part of if statement gets evaluated
在我的代码中,我遍历了 Twitch.tv 个用户名(称为用户)的数组,以便与每个用户进行 API 调用。其代码如下所示:
for(user in users){
var username = users[user];
console.log(username); //it iterates over every user in the array
cbJSON(username); //that means this function is called for each user
}
cbJSON 函数接受一个参数,即用户名,并进行 API 调用。然后,如果用户在线、离线或不存在,它应该写入控制台。为此,我使用了一个 if 语句来评估 data.stream 是否为真、空或未定义,如下所示:
function cbJSON(name){
$.getJSON(url + name + cb, function(data){
var ds = data.stream;
//Problem: Only the initial if statement works.
//The else if and else statements are NEVER called
//Only this works
if(ds){
//handle user is online
//show status, name, game, viewers, resolution
console.log(name + ' is online.');
//this is never called, even though it should be
} else if(ds === null) {
//handle user is offline
//show status, logo and name
console.log(name + ' is offline.');
//this is never called, even though it should be
} else {
//handle user does not exist
//error message: user does not exist
console.log(name + ' does not exist.');
}
});
}
只要 data.stream 存在且不为假,ds 应该计算为真。
当流离线时 ds 等于 null。
当通道不存在时,ds 等于 undefined。
如果这个 if 语句似乎得到了评估,只有第一部分。如果用户数组中的某人在线,'username is online' 将被写入控制台。但是,数组中的大多数用户都处于离线状态并且不存在,因此这些用户也应该以 'username is offline' 和 'username does not exist' 的形式写入控制台。
这是一个相当简单的 if 语句,我不认为我会 运行 在这里遇到任何麻烦,但我不明白为什么它没有按预期工作。我只是要 link 我的代码笔在这里,因为问题可能出在其他地方:http://codepen.io/erody-s/pen/WwagBo?editors=1011
还有一件事:if 语句在昨天工作,但从那以后我改变了一堆东西,包括 cbJSON 函数的调用方式,它已经停止正常工作了。
我对 JavaScript 还是有些陌生,所以我很可能在这里遗漏了一些明显的东西,但我自己无法弄清楚。相信我,我试过了。
如果有人能在这方面帮助我,我将非常高兴。
function cbJSON(name){
$.getJSON(url + name + cb, function(data){
var ds = data.stream;
console.log(ds); //
});
}
ds
是一个对象,它永远是真实的。您需要在 ds
上找到可以评估用户 online/offline 状态的 属性。
Object:
{
_id:21070202752,
_links:Object,
average_fps:60,
channel:Object,
created_at:"2016-04-30T08:02:01Z",
delay:0,
game:"Minecraft",
is_playlist:false,
preview:Object,
video_height:698,
viewers:299,
__proto__:Object
}
这是 ds
对象。
发现错误。
你在分配完ds后做了一堆作业
var ds = data.stream;
var linkStream = ds.channel.url;
var logo = ds.channel.logo;
var displayName = ds.channel.display_name;
var game = ds.game;
var viewers = ds.viewers;
var resolution = ds.video_height;
var preview = ds.preview.small;
你也说主播离线时ds应该是null吧?好吧,在 Javascript 中,如果您尝试获取 null 或未定义变量的 属性,您将抛出错误并且函数的其余部分将不会执行。
所以,我为你修好了笔:http://codepen.io/josepht404/pen/RaqbRw?editors=1012 现在可以用了:)。
希望对你有所帮助,谢谢。
在我的代码中,我遍历了 Twitch.tv 个用户名(称为用户)的数组,以便与每个用户进行 API 调用。其代码如下所示:
for(user in users){
var username = users[user];
console.log(username); //it iterates over every user in the array
cbJSON(username); //that means this function is called for each user
}
cbJSON 函数接受一个参数,即用户名,并进行 API 调用。然后,如果用户在线、离线或不存在,它应该写入控制台。为此,我使用了一个 if 语句来评估 data.stream 是否为真、空或未定义,如下所示:
function cbJSON(name){
$.getJSON(url + name + cb, function(data){
var ds = data.stream;
//Problem: Only the initial if statement works.
//The else if and else statements are NEVER called
//Only this works
if(ds){
//handle user is online
//show status, name, game, viewers, resolution
console.log(name + ' is online.');
//this is never called, even though it should be
} else if(ds === null) {
//handle user is offline
//show status, logo and name
console.log(name + ' is offline.');
//this is never called, even though it should be
} else {
//handle user does not exist
//error message: user does not exist
console.log(name + ' does not exist.');
}
});
}
只要 data.stream 存在且不为假,ds 应该计算为真。
当流离线时 ds 等于 null。
当通道不存在时,ds 等于 undefined。
如果这个 if 语句似乎得到了评估,只有第一部分。如果用户数组中的某人在线,'username is online' 将被写入控制台。但是,数组中的大多数用户都处于离线状态并且不存在,因此这些用户也应该以 'username is offline' 和 'username does not exist' 的形式写入控制台。
这是一个相当简单的 if 语句,我不认为我会 运行 在这里遇到任何麻烦,但我不明白为什么它没有按预期工作。我只是要 link 我的代码笔在这里,因为问题可能出在其他地方:http://codepen.io/erody-s/pen/WwagBo?editors=1011
还有一件事:if 语句在昨天工作,但从那以后我改变了一堆东西,包括 cbJSON 函数的调用方式,它已经停止正常工作了。
我对 JavaScript 还是有些陌生,所以我很可能在这里遗漏了一些明显的东西,但我自己无法弄清楚。相信我,我试过了。
如果有人能在这方面帮助我,我将非常高兴。
function cbJSON(name){
$.getJSON(url + name + cb, function(data){
var ds = data.stream;
console.log(ds); //
});
}
ds
是一个对象,它永远是真实的。您需要在 ds
上找到可以评估用户 online/offline 状态的 属性。
Object:
{
_id:21070202752,
_links:Object,
average_fps:60,
channel:Object,
created_at:"2016-04-30T08:02:01Z",
delay:0,
game:"Minecraft",
is_playlist:false,
preview:Object,
video_height:698,
viewers:299,
__proto__:Object
}
这是 ds
对象。
发现错误。
你在分配完ds后做了一堆作业
var ds = data.stream;
var linkStream = ds.channel.url;
var logo = ds.channel.logo;
var displayName = ds.channel.display_name;
var game = ds.game;
var viewers = ds.viewers;
var resolution = ds.video_height;
var preview = ds.preview.small;
你也说主播离线时ds应该是null吧?好吧,在 Javascript 中,如果您尝试获取 null 或未定义变量的 属性,您将抛出错误并且函数的其余部分将不会执行。 所以,我为你修好了笔:http://codepen.io/josepht404/pen/RaqbRw?editors=1012 现在可以用了:)。
希望对你有所帮助,谢谢。