Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Last active January 7, 2019 06:32
Show Gist options
  • Save joshuacerbito/e7d8d769575ec630cfe7a8927ffb2896 to your computer and use it in GitHub Desktop.
Save joshuacerbito/e7d8d769575ec630cfe7a8927ffb2896 to your computer and use it in GitHub Desktop.
Debounce + Throttle
export function debounce (fn, time) {
let timeout;
return function() {
const functionCall = () => fn.apply(this, arguments);
clearTimeout(timeout);
timeout = setTimeout(functionCall, time);
}
}
export function throttled (delay, fn) {
let lastCall = 0;
return function (...args) {
const now = (new Date).getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return fn(...args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment