Ber*_*Ber 0 javascript while-loop node.js
我正在努力使程序重复接受输入并重复它直到输入"退出".现在循环没有运行,我不知道为什么因为exit变量设置为false.这是我的代码:
var read = require("read");
var exit = false;
function OutsideLoop (exit) {
while(exit === false){
read({prompt: "> "}, function (err, result) {
console.log("");
console.log(result);
console.log("Type in more input or type 'exit' to close the program.");
if(result === "exit"){
exit = true;};
});
};
};
OutsideLoop();
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.我有一个类似的循环使用if/then而不是while,所以我沿着相同的行重写了这个.
您已将"exit"声明为函数的参数,因此外部声明对函数内部的逻辑没有影响.调用它时,不会向函数传递任何内容,因此"退出" undefined并且===测试失败.
如果你要么通过"退出"的功能,否则取出从函数声明的参数,它会工作- 也许.那个"读"函数是异步的,所以我不确定节点的行为方式.
| 归档时间: |
|
| 查看次数: |
113 次 |
| 最近记录: |