Skip to content
This repository

throttle function has bigger delays than it should #820

Closed
dcorb opened this Issue October 08, 2012 · 4 comments

3 participants

Corbacho Jeremy Ashkenas John-David Dalton
Corbacho

I found this minor bug when trying to make a demo to visualize throttle and debounce:

http://drupalmotion.com/article/debounce-and-throttle-visual-explanation

In certain cases, it will skip a execution and waits more (double wait time) than necessary.

I tracked it down to this scenario:

When executing a throttle'd function, there are 2 setTimeouts executed almost simultaneously
with exactly the same delay, AND in this execution order:

  • One for later()
  • One for whenDone() (internally in the _.debounce )

The problem is that later() will trigger a whenDone() also (aka, that same _.debounce)

Then, it can happen two things with that second _.debounce:

  • The first _debounce has finished. [Idealistic]
  • The first _debounce hasn't finished [Problematic], so the second _debounce will clearTimeout the first one.

I continue the bug report with an example in this text file that doesn't break the format:

http://drupalmotion.com/sites/default/files/demos/throttle.txt

Jeremy Ashkenas jashkenas closed this in bb97a4d October 09, 2012
Jeremy Ashkenas
Owner

Thanks for the report. I've updated the function to use effectively the same implementation as Lodash, removing the two potential timeouts.

Corbacho

Cool !. I updated the blog post to clarify that has been fixed in the master branch.

John-David Dalton

The updated throttle implementation also resolves issue #502 and helps stabilize existing throttle unit tests (the remaining calculation helps with timeout drift causing false test failings).

Don't forget unit tests (here and here).

Jeremy Ashkenas
Owner

Don't forget unit tests (here and here)

Hey, go for it. I've given you push access to the repo.

Christian G. Warden cwarden referenced this issue in pivotal/jasmine May 09, 2013
Closed

jasmine.Clock.useMock doesn't mock Date #361

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.