Skip to content

Instantly share code, notes, and snippets.

@remarcmij
Created February 15, 2022 09:37
Show Gist options
  • Save remarcmij/3056eee7bb0beac581fd5e98535df329 to your computer and use it in GitHub Desktop.
Save remarcmij/3056eee7bb0beac581fd5e98535df329 to your computer and use it in GitHub Desktop.
Plain vanilla JS debounce function
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