【问题标题】:for loop continues to run even when "break" is used即使使用了“break”,for循环也会继续运行
【发布时间】:2018-11-02 15:50:09
【问题描述】:

我正在尝试在一个文件夹中运行检查,如果有任何其他文件而不是扩展名 - '.txt',则该文件夹将只显示一个警报

这是我的代码:

for (let file of files) {
  const ext = path.extname(file);
  console.log(typeof file)
  if (ext != '.txt') {
    console.log(ext)
    console.log("The error should have popped up")
    dialog.showErrorBox('Non TXT file detected', 'there is a non-txt file in the folder')
    break
  }
  else {
    console.log("Not Working")
  }
}

我遇到的问题是说文件夹中有 10 个“.txt”文件,如果我将其中一个重命名为“.test”,我会收到 10 个警报。有没有办法在“dialog.showErrorBox”被调用后立即阻止 for 循环继续。

无论我更改哪个文件的扩展名,我都会收到相同数量的错误。

【问题讨论】:

标签: javascript node.js electron


【解决方案1】:

我复制了你的代码,它似乎工作正常:https://repl.it/repls/ExcellentVapidApplicationframework

正如 Andreas 建议的那样,可能是由 ASI 引起的问题?

【讨论】:

  • 提供的代码中缺少分号没有任何问题。
猜你喜欢
  • 2016-09-10
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2022-11-27
  • 1970-01-01
  • 2019-08-09
相关资源
最近更新 更多