gpt4 book ai didi

javascript - Node setTimeout 不工作

转载 作者:行者123 更新时间:2023-11-30 00:19:50 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com