Last active
September 6, 2019 04:33
-
-
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.
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(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