-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Allow _.throttle to function correctly after the system time is updated #1473
Allow _.throttle to function correctly after the system time is updated #1473
Conversation
This is mainly useful for things like Daylight Savings Time or going between time zones, right? Otherwise this seems like an incredibly slight edge case. |
Seems like Perhaps it's worth considering removing |
@akre54 This is one case, the project where I use this is a browser game based in many countries. Apparently players will adjust the system clock to match their local time difference to the servers. @smelnikov Unfortunately yes, when the system time changes this fix may trigger a callback prematurely but I believe this is not a huge issue, as most wait offsets are small and after the initial callback, the timer will be cleared and state will return to normal. This fix could also be applied to |
This is probably too edge-case for Underscore. You might |
Nah, this is a thing that can happen. Especially for long-lived single page apps that you don't close. Let's add the sanity check fix for this and for debounce. |
|
When _.throttle has a delay time of 24 hours? Now that's an edge case, in the browser ;) |
You know what I mean. Any time over an hour, or in the case of a leap second, any time over a second. |
I believe the this won't be an issue, after reading ECMAScript Spec §15.9.1.1 it appears timestamps from Taking this into account, I have also added a sanity check to |
It would appear that this fix also fixes issues with Sinon.js, specifically tests on debounced functions that use sinon.useFakeTimers (https://github.com/cjohansen/Sinon.JS/blob/master/lib/sinon/util/fake_timers.js) |
I spoke to soon. It seems that there are random failures with useFakeTimers on debounced functions with underscore 1.6 |
@grahamscott you can find more info on the fake timers issue at #1478, if you haven't seen it already. |
Thank you! |
Allow _.throttle to function correctly after the system time is updated
I have found that if the system time is moved backwards, calls to throttle will stop executing.
To test this, if the system time is moved backwards by an hour then the
remaining
variable in_.throttle
containing the time offset becomes a large positive integer and the throttle callback will not be called until the adjusted hour time difference has passed, instead of the expected wait time.For this reason I suggest that an additional check is made to see if
remaining
exceeds thewait
time which will then clear the existing timeout and return the method to a normal state.I have tested this in a project and provided a unit test as proof.