Created
August 18, 2017 13:16
-
-
Save rokobuljan/846fe87230d857815380fd36cba54365 to your computer and use it in GitHub Desktop.
JS Throttle Debounce Functions
This file contains hidden or 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 throttle (cb, delay) { | |
let prevCall = +new Date(); | |
return () => { | |
const time = +new Date(); | |
if ((time - prevCall) >= delay) { | |
prevCall = time; | |
cb.apply(null, arguments); | |
} | |
}; | |
} | |
function debounce (cb, delay) { | |
let tOut = null; | |
return () => { | |
clearTimeout(tOut); | |
tOut = setTimeout(() => { | |
cb.apply(this, arguments); | |
}, delay); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment