Search code examples
javascriptnode.jsserversettimeout

How long can NodeJS `setTimeout` wait?


Can NodeJS setTimeout delay excecution of function for a week? (assuming the server doesnt go down...) In some other servers like ASP.NET CORE, the server will sleep when not in use, hence we can't use such.

Does the same happen in the NodeJS world, or the server remains on forever?


Solution

  • Answering your question

    setTimeout has the second argument of delay as a 32-bit signed integer. So the value can not be greater than 2147483647 (about 24.8 days). When the delay is larger than 2147483647, then the delay will set to 1. (ref)

    Answering your use-case

    Instead of using setTimeout for such a long delay, you can run cron job.