简体   繁体   English

NodeJS`setTimeout`可以等多久?

[英]How long can NodeJS `setTimeout` wait?

Can NodeJS setTimeout delay excecution of function for a week? NodeJS setTimeout延迟一周的功能吗? ( 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. 假设服务器没有停机...... )在ASP.NET CORE等其他服务器中,服务器将在不使用时休眠,因此我们无法使用。

Does the same happen in the NodeJS world, or the server remains on forever? 在NodeJS世界中也会发生同样的情况,或者服务器是否永远存在?

There is nothing in the documentation that would suggest it would not work. 文档中没有任何内容表明它不起作用。 However, if the length in millisecond is greater than 2147483647 (24 day 20 h 31 min 24 s), the delay is set to 1. 但是,如果以毫秒为单位的长度大于2147483647 (24天20小时31分钟24秒),则延迟设置为1.

https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args

The behavior is different on a browser. 浏览器的行为是不同的。 Unsurprisingly, the timeout is delayed if the associated tab is inactive. 不出所料,如果关联的选项卡处于非活动状态,则超时会延迟。

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). 如果方法上下文是Window对象,请等待与方法上下文关联的Document完全处于活动状态超过毫秒(不一定是连续)。

Otherwise, if the method context is a WorkerUtils object, wait until timeout milliseconds have passed with the worker not suspended (not necessarily consecutively). 否则,如果方法上下文是WorkerUtils对象,请等待,直到超时毫秒已经过去,而工作程序未挂起(不一定是连续)。

https://www.w3.org/TR/2011/WD-html5-20110525/timers.html#dom-windowtimers-settimeout https://www.w3.org/TR/2011/WD-html5-20110525/timers.html#dom-windowtimers-settimeout

Answering your question 回答你的问题

setTimeout has the second argument of delay as a 32-bit signed integer. setTimeout将延迟的第二个参数作为32位有符号整数。 So the value can not be greater than 2147483647 (about 24.8 days). 所以该值不能大于2147483647(约24.8天)。 When the delay is larger than 2147483647, then the day will set to 1. ( ref ) 当延迟大于2147483647时,那一天将设置为1.( 参考

Answering your use-case 回答你的用例

instead of using setTimeout for such a long delay, you can run cron job. 而不是使用setTimeout这么长的延迟,你可以运行cron job。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM