mini-throttle icon indicating copy to clipboard operation
mini-throttle copied to clipboard

middle: true with throttle

Open stefnotch opened this issue 4 years ago • 2 comments

Maybe I misunderstood something, but the current default behaviour of middle: true seems a bit surprising to me.

Currently, if one calls the function for the first time, it will get invoked. Then, if the function gets called again while the delay is still in place, it will get deferred until the next time. And finally, if the function gets called after the delay is over, nothing seems to happen.

Here is a little snippet to reproduce this

x = throttle(() => console.log("called"), 1000) // throttle 1 second

x() // call x
setTimeout(() => {console.log("x doesn't happen"); x(); }, 5000) // call again in 5 seconds

stefnotch avatar Jun 18 '21 17:06 stefnotch

I can reproduce.

Looking through the source the variable start should be reset to true sometime but doesn't happen.

luwes avatar May 08 '22 01:05 luwes

Sorry I missed the OP somehow.

If anyone would like to tackle this as a contribution, PRs are welcome!

keithamus avatar May 08 '22 08:05 keithamus

Hi @keithamus,

I have made a PR to fix this. #15

percy507 avatar Feb 17 '23 02:02 percy507