【问题标题】:If statement with logical or operator带有逻辑或运算符的 if 语句
【发布时间】:2017-12-16 20:01:21
【问题描述】:

我希望我的函数检查状态是“关闭”还是“完成”,以及是否是其中任何一个来执行括号中的操作。

由于某种原因,我似乎无法完成这项工作,它会检查已关闭但未检查完成。

for (var id in games) {
if (games[id].league === 'NCAAB')
if ((games[id].status == 'complete')||(games[id].status == 'closed')) {
  sports.clearTimer();
}
    if (ts >= games[id].scheduledTimeUnix) { xxxxxx}

尽管事实上只有两个节点的状态是完成和关闭,但现在计时器没有被清除。 XXXX 代表发生的 api 调用,并且函数不断进行这些调用。

【问题讨论】:

  • 没有minimal reproducible example,我们无法不经过大量猜测就知道原因
  • 值真的是complete吗?例如,不是completed吗?
  • 它应该以这种方式工作(在多余的括号旁边)。
  • 可能是因为if (games[id].league === 'NCAAB') 的计算结果为false
  • 首先使用您的调试器查看正在遵循的代码路径以及 games[id].status 的值是什么。或者,您可以暂时插入console.log(games[id].status) 并注意Complete 或拼写错误。它可能是未定义的。或者,也许您的属性名称错误。例如,可能是 .state 而不是 .status 或类似的东西。

标签: javascript node.js


【解决方案1】:

or 检查是否为真,如果其中一个为真,则不会检查其他条件并执行它之前的操作。

if ((games[id].status == 'closed')||(games[id].status == 'complete'))

如果你得到了

games[id].status == 'closed'

如果为真,它不会检查

(games[id].status == 'complete')

【讨论】:

  • 你没有回答这个问题。 OP 不要问OR 是如何工作的,OP 对status == 'complete' 有问题 - 因为某种原因没有清除计时器。
猜你喜欢
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多