admin管理员组文章数量:1277303
In my Node.js application, I use setInterval()
to run a specific function every 1 hour. The function is executed properly for about 25 days, then the timer stops firing.
25 days seems awfully close to Node.js's TIMEOUT_MAX
(2^31 milliseconds ≈ 25 days), but I don't really see why setInterval()
should stop executing after that time.
Update:
I think this may have been caused by the following bug in Node.js: setInterval callback function unexpected halt #22149
In my Node.js application, I use setInterval()
to run a specific function every 1 hour. The function is executed properly for about 25 days, then the timer stops firing.
25 days seems awfully close to Node.js's TIMEOUT_MAX
(2^31 milliseconds ≈ 25 days), but I don't really see why setInterval()
should stop executing after that time.
Update:
I think this may have been caused by the following bug in Node.js: setInterval callback function unexpected halt #22149
Share Improve this question edited Aug 14, 2018 at 7:40 Moorglade asked Aug 14, 2018 at 6:51 MoorgladeMoorglade 3162 silver badges8 bronze badges 11-
Have you tried using a recursive
setTimeout
instead? – CertainPerformance Commented Aug 14, 2018 at 6:52 - 2 What is your code? – user202729 Commented Aug 14, 2018 at 6:54
- stackoverflow./questions/51826266/… – user202729 Commented Aug 14, 2018 at 6:56
-
1
Could you use something like
cron
/schtasks
/ some other OS-level scheduler instead? – Phil Commented Aug 14, 2018 at 6:57 - 1 @NoobTW I don't know about node-cron, but we were using node-schedule before and had the exact same problem. Anyway, it seems it's been fixed in Node.js now. – Moorglade Commented Aug 16, 2018 at 6:07
3 Answers
Reset to default 6It seems the bug (#22149) has been fixed in Node.js 10.9.0.
It may be also worth noting that this bug seems to be influencing both setInterval()
and setTimeout()
(as reported here), so the workaround with setTimeout()
in the callback function wouldn't work.
After reading the issue in github, I think they will fix it in the next release. If you need to work around before that, you can try recursive timeout instead. Checkout my code:
function doSomething() {
console.log('test');
}
function doSomethingInterval() {
doSomething();
setTimeout(doSomethingInterval, 1000);
}
doSomethingInterval();
It's a known bug. Your easiest workaround is to use setTimeout instead, and then in your callback function call another setTimeout.
本文标签: javascriptNodejs setInterval() stops executing after 25 daysStack Overflow
版权声明:本文标题:javascript - Node.js setInterval() stops executing after 25 days - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741265195a2368328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论