Skip to content

useThrottle - With continuous inputs, the throttle does not work #341

Open
@eTallang

Description

@eTallang

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions