Created
February 15, 2022 09:37
-
-
Save remarcmij/3056eee7bb0beac581fd5e98535df329 to your computer and use it in GitHub Desktop.
Plain vanilla JS debounce function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce(fn, time) { | |
let timeoutId; | |
return (...args) => { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(fn, time, ...args); | |
}; | |
} | |
function foo(msg) { | |
console.log(msg); | |
} | |
const bar = debounce(foo, 2000); | |
bar('Hello world'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment