Last active
May 3, 2018 14:02
-
-
Save wiseoldman/92f43ad7850de4cd9cc95ab613eb4f8f to your computer and use it in GitHub Desktop.
[Debounce] Small module to debounce (e.g. scroll and resize functions) #debounce
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, 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 }; |
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
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