Last active
June 11, 2018 07:16
-
-
Save michaelmano/832dce5f0a026678387c1f4048b9fa25 to your computer and use it in GitHub Desktop.
ES6 Callback 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
"use strict" | |
/** | |
* window.addEventListener('scroll', debounce(() => console.log('Window scrolled.'))); | |
*/ | |
const debounce = function debounce(callback, delay = 250) { | |
let timeout = null; | |
return () => { | |
clearTimeout(timeout); | |
timeout = setTimeout(() => callback.apply(this, arguments), delay); | |
}; | |
}; | |
export default debounce; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment