Coding Challenge - #20: Debounce Function

Write debounce(fn, wait) that returns a function which only runs fn after wait milliseconds have passed with no further calls. Calling it repeatedly should reset the timer.

function debounce(fn, wait) {
  // your code here
}

Rules:

  • Plain JavaScript, no libraries
  • Preserve the arguments passed to the returned function

Post your solution as a reply. Answer goes up in about a day.