Skip to content

Instantly share code, notes, and snippets.

@message
Created April 29, 2015 13:40
Show Gist options
  • Save message/abd151ee2a49a42e8985 to your computer and use it in GitHub Desktop.
Save message/abd151ee2a49a42e8985 to your computer and use it in GitHub Desktop.
throttle
[4:20:11 PM] Dmitry Polovka: what's the purpose of debouncer/throttle? Just to debounce single function for ...ms, and if you will debounce same func twice, it will be called twice?
Or for example if
function x is debounced for 500ms
after 200ms... function x is debounced for 500ms once again
it will be called once after 500ms, and will not be added as second function call?
[4:32:55 PM] @Касперский: There are two strategies for debounce and throttle, but what common is that original function will never be called more than once in X ms no matter how many times you call throttle / debounced function.
Throttle:
One of the strategies is to call immediately on throttle function call (e.g. with 500ms), called "leading":
throttle function called at 0ms: original function called
throttle function called at 100ms: nothing
throttle function called at 400ms: nothing
throttle function called at 600ms: original function called
Another is to call after, called "tailing":
throttle function called at 0ms: nothing
throttle function called at 100ms: nothing
throttle function called at 400ms: nothing
timer hit 500ms: original function called
throttle function called at 600ms: nothing
timer hit 1100ms: original function called
Subsequent calls to throttle during X ms period does nothing and function will be called only once.
Debounce:
Debounce is similar, except original function will be called X ms after LAST call to debounce function, not like with throttle where original function is called X ms after FIRST throttle function call
[4:34:11 PM] @Касперский: Check:
https://lodash.com/docs#throttle
https://lodash.com/docs#debounce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment