Implement a Debounce Function That Delays Execution Until Input Settles
Write a debounce(fn, delay) function that returns a new function which, when called repeatedly, only executes fn after the specified delay has passed since the last call — any call within the delay window resets the timer. This prevents expensive operations (API calls, DOM updates, search queries) from firing on every keystroke. Cover edge cases like immediate invocation, cancellation, and clearing the debounced timer.