-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: add note about timeout delay > TIMEOUT_MAX #3512
Conversation
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. This adds a note about this in the timers docs.
@@ -16,6 +16,8 @@ It is important to note that your callback will probably not be called in exactl | |||
the callback will fire, nor of the ordering things will fire in. The callback will | |||
be called as close as possible to the time specified. | |||
|
|||
To follow browser behavior, when using delays larger than 2147483647 milliseconds (approximately 25 days), the timeout is executed immediately, as if the `delay` was set to 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe say "If delay
is less than one, or greater than the maximum allowed value of 2147483647, it will be assigned a value of 1."
@@ -16,6 +16,8 @@ It is important to note that your callback will probably not be called in exactl | |||
the callback will fire, nor of the ordering things will fire in. The callback will | |||
be called as close as possible to the time specified. | |||
|
|||
To follow browser behavior, when using delays larger than 2147483647 milliseconds (approximately 25 days) or less than 1, the timeout is executed immediately, as if the `delay` was set to 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style nit: word wrap 80 chars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ops, sorry
Thanks for the added doc changes. LGTM. @nodejs/tsc I don't believe using |
@trevnorris I don't mind, though we will need to document the lost of precision for the values greater than |
Okay. I'll leave this open and try to get to a fix by the end of the week. |
+1 if browser consistency is the only reason it's implemented like this. Would this only affect someone trying to run Node code in the browser? |
@cjihrig Think so. But at that point it's really UB, since in setTimeout(()=>console.log('hi'),(-1 >>> 1) + 1); EDIT: Firefox doesn't throw. It also wraps. |
Browsers don't wrap (in the modulo sense), they expire the timer immediately. If you enter the following in the web console: > setTimeout(() => console.log('two'), 60e3 + Math.pow(2,31)); setTimeout(() => console.log('one'), Math.pow(2,31))
1
two
one While with modulo arithmetic it would print "one two" (with a 60 second delay between them.) I added TIMEOUT_MAX way back because someone complained node was different from the browser, so apparently it's relevant at least to some people. |
I'd suggest just making an npm module that wraps timeout and spreads it out across multiple timeouts if you need anything larger. |
K. Then let's land this so devs realize a module should be made. :-) |
If anyone from the future reads this, there are already a few such modules out there. |
PR LGTM |
Thanks. Landed on ac7dd5f. |
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. Add a note about this in the timers docs. PR-URL: #3512 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rich Trott <[email protected]>
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. Add a note about this in the timers docs. PR-URL: #3512 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rich Trott <[email protected]>
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. Add a note about this in the timers docs. PR-URL: #3512 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rich Trott <[email protected]>
landed in lts-v4.x-staging as d02365b |
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. Add a note about this in the timers docs. PR-URL: #3512 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rich Trott <[email protected]>
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. Add a note about this in the timers docs. PR-URL: #3512 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rich Trott <[email protected]>
When setTimeout() and setInterval() are called with `delay` greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. Add a note about this in the timers docs. PR-URL: #3512 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rich Trott <[email protected]>
When setTimeout() and setInterval() are called with
delay
greater than TIMEOUT_MAX (2147483647), the supplied value is ignored and 1 is used instead. This adds a note about this in the timers docs.I actually got surprised by this behavior when trying to schedule a job to run in the first day of every month:
It worked the first time, but afterwards it seemed to enter an infinite loop. I only found out why after searching and reading the timers.js code.
It only worked at first because it was past the 5th day of that month ;)