function work(){
//do some of the heavy computation now
process.nextTick(work);
}
work();
console.log("started");
setTimeout(function(){ console.log("hi there");}, 1000);
当我运行他的代码时,我得到了 : started,但我从来没有收到消息 hi there。 我做错了什么,还是 Node 有问题?
编辑: 当我将 process.nextTick 替换为 setTimeout 时,它可以工作,但 nextTick 应该更快,或者当我将 setTimeout 替换为 process.nextTick() hello 显示时,它不会等待 1000 毫秒。
最佳答案
使用 setImmediate 是可行的方法
function work(){
setImmediate(work);
}
work();
console.log("started");
setTimeout(function(){ console.log("hi there");}, 1000);
关于javascript - Node setTimeout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33584060/