AniUI Academy
medium+250 XPEnded 3 Aug

Write debounce from scratch

A debounced function waits until the calls stop coming before it actually runs. Type ten characters quickly and the search fires once, not ten times.

Implement debounce(fn, wait) returning a new function that delays calling fn until wait milliseconds have passed without another call.

What it has to do

  • Reset the timer every time the debounced function is called again.
  • Call fn with the arguments from the most recent call.
  • Preserve this when the debounced function is used as a method.
  • Expose a .cancel() that stops a pending call.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

Stuck? The javascript course covers everything this challenge needs.