使用nodejs在一定时间后运行函数/代码

Jer*_*Fox 14 javascript timer delay settimeout node.js

我正在寻找一种在N秒后在nodejs中运行一些代码的方法.

尝试了setTimeout(),但它似乎完全阻止它,直到时间结束但这不是我想要的,因为我的服务器仍在发送和接收事件.

有什么建议?

小智 18

实际上,setTimeout是异步的,所以它不会阻塞.

setTimeout(function(){
    // this code will only run when time has ellapsed
}, n * 1000);
// this code will not block, and will only run at the time
Run Code Online (Sandbox Code Playgroud)