Home
← All questions

Advanced debounce with leading and trailing options in JavaScript

HardPremiumJavaScript

Asked at MindTickle

JavaScript interview practice: Implement debounce supporting { leading, trailing } options to control when the function fires.

Problem

Implement debounce(fn, wait, options) where options is { leading, trailing }:

  • leading: true → invoke on the first call of a burst (immediately).
  • trailing: true → invoke after the burst settles (default).
  • both true → invoke at the start and end of a burst.
function debounce(fn, wait, { leading = false, trailing = true } = {}) {
  // your code
}
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium