logo
logo

Basic Debounce

Medium

Basic Debounce

Medium

Write a function that takes in a callback function and a delay time. The function should return a new function that can only be called once per delay time. If the function is called more than once within the delay time, it should only execute the callback function once, after the delay time has passed.

For example,
const debouncedLog = debounceEventLogger(() => console.log('Hello world!'), 1000);
would return a new function that logs "Hello world!" to the console only once, even if it is called multiple times within the 1000ms delay time.
Console
Submit
Solution
00:00

Solution Walkthrough for Basic Debounce

In the
debounceEventLogger
function, we are using the
apply
method to call the original
callback
function with the same context and arguments that were passed to the debounced function.
The context, represented by the
this
keyword, is determined by the execution context in which the function is called. In the case of the debounced function, the execution context will be the same as the original function that was passed in.
So, in short, the
apply
method is used to call the
callback
function with the same context and arguments that were passed to the debounced function. This ensures that the
callback
function is executed with the correct context and arguments, even though it is being called by a different function.