NodeJS `setTimeout` 可以等待多长时间?
How long can NodeJS `setTimeout` wait?
NodeJS setTimeout
函数可以延迟一周执行吗? (假设服务器没有宕机...)
在其他一些服务器中,如 ASP.NET CORE,服务器在不使用时会休眠,因此我们不能使用这样的服务器。
在 NodeJS 世界中是否会发生同样的情况,或者服务器会永远保持运行状态?
文档中没有任何内容表明它不起作用。但是,如果以毫秒为单位的长度大于 2147483647
(24 天 20 小时 31 分 24 秒),则延迟设置为 1.
https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args
行为在浏览器上有所不同。毫不奇怪,如果关联的选项卡处于非活动状态,则超时会延迟。
If the method context is a Window object, wait until the Document
associated with the method context has been fully active for a further
timeout milliseconds (not necessarily consecutively).
Otherwise, if the method context is a WorkerUtils object, wait until
timeout milliseconds have passed with the worker not suspended (not
necessarily consecutively).
https://www.w3.org/TR/2011/WD-html5-20110525/timers.html#dom-windowtimers-settimeout
Answering your question
setTimeout 的第二个参数 delay 是一个 32 位有符号整数。所以该值不能大于2147483647(约24.8天)。当延迟大于2147483647时,天将设置为1。(ref)
Answering your use-case
您可以 运行 cron 工作,而不是使用 setTimeout
进行如此长的延迟。
NodeJS setTimeout
函数可以延迟一周执行吗? (假设服务器没有宕机...)
在其他一些服务器中,如 ASP.NET CORE,服务器在不使用时会休眠,因此我们不能使用这样的服务器。
在 NodeJS 世界中是否会发生同样的情况,或者服务器会永远保持运行状态?
文档中没有任何内容表明它不起作用。但是,如果以毫秒为单位的长度大于 2147483647
(24 天 20 小时 31 分 24 秒),则延迟设置为 1.
https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args
行为在浏览器上有所不同。毫不奇怪,如果关联的选项卡处于非活动状态,则超时会延迟。
If the method context is a Window object, wait until the Document associated with the method context has been fully active for a further timeout milliseconds (not necessarily consecutively).
Otherwise, if the method context is a WorkerUtils object, wait until timeout milliseconds have passed with the worker not suspended (not necessarily consecutively).
https://www.w3.org/TR/2011/WD-html5-20110525/timers.html#dom-windowtimers-settimeout
Answering your question
setTimeout 的第二个参数 delay 是一个 32 位有符号整数。所以该值不能大于2147483647(约24.8天)。当延迟大于2147483647时,天将设置为1。(ref)
Answering your use-case
您可以 运行 cron 工作,而不是使用 setTimeout
进行如此长的延迟。