Skip to content

Instantly share code, notes, and snippets.

@rokobuljan
Created August 18, 2017 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokobuljan/846fe87230d857815380fd36cba54365 to your computer and use it in GitHub Desktop.
Save rokobuljan/846fe87230d857815380fd36cba54365 to your computer and use it in GitHub Desktop.
JS Throttle Debounce Functions
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