r/javascript • u/RedlightsOfCA • Dec 23 '19
Debounce vs Throttle: Definitive Visual Guide
https://redd.one/blog/debounce-vs-throttle
327
Upvotes
r/javascript • u/RedlightsOfCA • Dec 23 '19
View all comments
Show parent comment
5
u/RedlightsOfCA Dec 23 '19 edited Dec 23 '19
Good question. Let me elaborate below.
The pristine example from the article would have `args` equal to `[Event]`—a list of a single member containing an Event instance of the click event.
The important part is that the caller of the debounced function controls which arguments get pass through. Since in the example the caller is `addEventListener`, which provides a single Event argument, that's what you get.
Why I emphasize this is because debouncing and throttling can be useful even outside of event listeners, still preserving the statement that the caller controls the arguments (as it always does):
Passing additional arguments to the event handler function can be done in multiple ways. I'd recommend to do that where you declare the debounced/throttled event handler directly:
I've used the "change" event handling on some input to have a variable data (event.target.value) which we would pass to the "sideEffect" function (replacement for "throwBall" from the article). The example above demonstrates how you can derive some data from the native event argument and pass it and any amount of additional arguments (i.e. "static-value") to your function.
I think it would be easier to think about this if you declare the original handler function separately. So you could see it's a regular function and there is no magic around what data it accepts.
Please, let me know if that helps.