Skip to content

Instantly share code, notes, and snippets.

@wiseoldman
Last active May 3, 2018 14:02
Show Gist options
  • Save wiseoldman/92f43ad7850de4cd9cc95ab613eb4f8f to your computer and use it in GitHub Desktop.
Save wiseoldman/92f43ad7850de4cd9cc95ab613eb4f8f to your computer and use it in GitHub Desktop.
[Debounce] Small module to debounce (e.g. scroll and resize functions) #debounce
function debounce (func, wait, immediate) {
let timeout;
return () => {
const context = this;
const args = arguments;
let later = () => {
timeout = null;
if (!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
module.exports = { debounce };
const DEBOUNCE_THIS = debounce(() => {
console.log('DEBOUNCE_THIS');
}, 500);
window.addEventListener('resize', DEBOUNCE_THIS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment