Run function/code after certain time with nodejs

JerryFox picture JerryFox · Jul 8, 2014 · Viewed 11.5k times · Source

I am looking for a way to run some code in nodejs after N amount of seconds.

Tried setTimeout() but it seems to completely block on it until the time is out but this is not what I want since my server is still sending and receiving events.

Any advice?

Answer

Wio picture Wio · Jul 8, 2014

Actually, setTimeout is asynchronous, so it will not block.

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