usehooks icon indicating copy to clipboard operation
usehooks copied to clipboard

useThrottle - With continuous inputs, the throttle does not work

Open eTallang opened this issue 4 months ago • 1 comments

Hi!

I'm using the useThrottle hook, and to me, the implementation of the hook is a bit confusing. My idea of a throttle function, is a function that accepts lots of inputs over time, and outputs the latest value each desired interval.

Say that we input the letters "A" to "G" with a delay of 500ms between each letter, and we throttle it to 2000ms. This means that the input would be the following (with the delay in parentheses):

  • A (0)
  • B (500)
  • C (1000)
  • D (1500)
  • E (2000)
  • F (2500)
  • G(3000)

My assumption is that the output would be A, E and G. A is the initial value, the E should be included since it is emitted 2000ms after the initially accepted value (A), and G should be emitted after 4000ms (since it is 2000ms after the E). How the useThrottle hook works today, is that it simply starts a new timeout each time a new value is received. This means that the output becomes A and G.

Is the hook wrong (maybe the lastUpdated useRef should have an intial value of Date.now()?), or am I misunderstanding the throttle concept here?

eTallang avatar Jun 17 '25 14:06 eTallang