Basic Debounce
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.
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.Solution Walkthrough for Basic Debounce
Spoiler Alert!
Don't read this unless you've already solved the problem.
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.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.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.