Debounce a Function

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write debounce(fn, wait): return a function that, every time it's called, starts the wait of wait milliseconds over again. Once a whole wait passes with no further calls, it calls fn once, with the arguments of the latest call. The returned function returns undefined and has a cancel() method that drops any pending call; it keeps working after a cancel. The checks use waits of 50 to 60 milliseconds.

Your code must pass

  • Calls fn once, with the latest arguments, after the wait
  • Keeps waiting while calls keep coming
  • Calls again for a later burst
  • cancel() drops the pending call