-
Notifications
You must be signed in to change notification settings - Fork 195
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
Implement throttle, move findDOMNode call #63
Conversation
Throttle is similar to a debounce but it limits a function to be called a max number of times per time limit, instead of just delaying the call. Also moved the findDOMNode call to didMount, since not necessary to call every time Check is called
I think a throttle function is probably the expected behavior for a scroll function vs using debounce. This throttle calls the function on the tail end of the timeout. I’ll document if you agree with the implementation |
Thanks @ChristianDavis ! Will try this out soon when I have an opportunity. |
@@ -22,10 +22,6 @@ | |||
], | |||
"author": "joshwnj", | |||
"license": "MIT", | |||
"peerDependencies": { |
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.
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.
Forgot I had removed that. It doesn't really need to be removed, but those packages are a devDependency if the example is to be built. peerDependencies was changed in NPM 3, doesn't install the package just issues warnings. The peer part can remain as a duplicate
if (!wait) { | ||
wait = true; | ||
setTimeout(function () { | ||
callback.call(); |
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.
Out of curiosity is there any reason for callback.call()
instead of just callback()
?
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.
No, I grabbed someone else's implementation, and there are no arguments here. Should just be callback()
I initially implemented throttle myself before doing this pull request: -> #54 (comment) Unfortunately it didn't work well in my case (that's why I preferred debounce), triggering at the beginning but not also at the end if the time is lower than the time set in the throttle. Unexpected behaviour may arise. I see both are available now, so that's ok :D |
@eek Yeah, controlling scroll is tough. Lots of specific tweaks required. I think using the long interval with an active check is a great approach. But with debounce, you can scroll right past an event without ever triggering it. Throttle was required for my use, what did you mean by
|
Hey! Sorry for the late-reply, I haven't tested your code but look a bit over it, it seems more like a combination between debounce and throttle, which is really great, I also tested a throttle function, but throttle usually triggers every x milliseconds starting from the beginning, so if it was 1s delay, and I would scroll for 0.9s, it would have triggered just the first time. Your function is great because it also triggers afterwards, I would actually propose to replace debounce with this. Hah |
Before we merge this, can someone try to reproduce #66? It might be a simple matter of adding |
Throttle is similar to a debounce but it limits a function to be called
a max number of times per time limit, instead of just delaying the
call. Also moved the findDOMNode call to didMount, since not necessary
to call every time Check is called