Skip to content
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

Fix useThrottle timing #302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dumbmatter
Copy link

This fixes a couple issues that lead to subtle timing bugs in useThrottle. For an example, see this codesandbox and notice how throttledValue (current implementation) updates kind of choppily and drops some values, whereas throttledValue2 (includes this fix) works as you'd expect.

First fix is that the now variable inside setTimeout does not actually refer to the current time, it refers to the time setTimeout was called. That means lastUpdated.current will be incorrect (too low) for the next update. Instead we should get a new Date.now() value.

Second fix is that the setTimeout is currently delayed by the full value of interval, even if there is only like a millisecond left until the interval elapses. Fix is to instead delay it by interval minus the time since the last update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant