Skip to content

Instantly share code, notes, and snippets.

@sslotsky
Created September 14, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sslotsky/e95bb205f3121e537f1d6a99c99593da to your computer and use it in GitHub Desktop.
Save sslotsky/e95bb205f3121e537f1d6a99c99593da to your computer and use it in GitHub Desktop.
function debounce(fn, interval) {
let timeout;
return function(...args) {
clearTimeout(timeout);
return new Promise(resolve => {
timeout = setTimeout(() => resolve(fn(...args)), interval);
});
}
}
const debounced = debounce(() => console.log("hello"), 200);
window.addEventListener('keydown', () => {
debounced().then(() => console.log("well hello to you too!"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment