Skip to content

Instantly share code, notes, and snippets.

@JenniferFuBook
Last active September 6, 2019 04:33
Show Gist options
  • Save JenniferFuBook/dce897550feef47e13dbc2c75e059733 to your computer and use it in GitHub Desktop.
Save JenniferFuBook/dce897550feef47e13dbc2c75e059733 to your computer and use it in GitHub Desktop.
Debounce limits the rate at which a function can fire. It enforces certain amount waiting time before a function is actually invoked. It is a different control compared to throttle.
function debounce(func, wait) {
let current;
return function (...args) {
clearTimeout(current);
current = setTimeout(() => func.apply(this, args), wait);
}
}
// The message, 'Function is actually invoked', will be printed 1 second after the browser window stops resizing.
window.addEventListener('resize', debounce(()=>console.log('Function is actually invoked'), 1000));
console.log('Debounce will be called');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment