Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

throttle function has bigger delays than it should #820

Closed
dcorb opened this Issue · 4 comments

3 participants

@dcorb

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

@jashkenas jashkenas closed this in bb97a4d
@jashkenas
Owner

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

@dcorb

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

@jdalton

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).

@jashkenas
Owner

Don't forget unit tests (here and here)

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

@cwarden cwarden referenced this issue in jasmine/jasmine
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.