6 - Debounce

hard
javascript
closure

Implement a simple debounce function in JavaScript. The function should take another function as its argument and return a new function that delays invoking the original function until after a specified time has elapsed since the last time the new function was invoked. Ensure that the debounced function is not called more than once within the specified time interval.

Example

function originalFunction() {
  console.log("Function invoked!");
}

const debouncedFunction = debounce(originalFunction, 500);

// The original function should be invoked 

//only once after 500 milliseconds

debouncedFunction(); // No immediate output

debouncedFunction(); // No immediate output


// Wait for 500 milliseconds

// Output: "Function invoked!"

⚙️

Booting up the editor...

Please wait while we load the editor.

⚙️

Loading Preview...