Implement a function to debounce another function, delaying its execution until after a specified interval.
HardProblem Statement
Implement a function to debounce another function, delaying its execution until after a specified interval.
Sample Input
Function to be debounced: logMessage Debounce interval: 300 ms Number of times logMessage is called in a row: 3
Sample Output
Only one output after 300ms of inactivity.
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Implement debouncing to delay function execution
function debounce(func, delay) {
// Initialize a timer variable
// Return a function that clears the timer and sets a new one
}
// Example function to debounce
function logMessage() {
// Log a message to the console
}
const debouncedLog = debounce(logMessage, 300);
debouncedLog();
debouncedLog();
debouncedLog();
Input
Function to be debounced: logMessage Debounce interval: 300 ms Number of times logMessage is called in a row: 3
Expected Output
Only one output after 300ms of inactivity.