Skip to content

Instantly share code, notes, and snippets.

@gtwalford
Created February 3, 2017 20:11
Show Gist options
  • Save gtwalford/f1dd58f4e6bb90b31aee86df82cca880 to your computer and use it in GitHub Desktop.
Save gtwalford/f1dd58f4e6bb90b31aee86df82cca880 to your computer and use it in GitHub Desktop.
Throttle / Debounce example
const _throttle = (fn, timeout, opts) => {
let pid = null;
return () => {
if(pid) { return }
pid = setTimeout(fn, timeout);
};
};
const _debounce = (fn, timeout, opts) => {
let pid = null;
return () => {
if(pid) {
clearTimeout(pid);
}
pid = setTimeout(fn, timeout);
};
};
const t = _throttle(
() => {
console.log('Whitney is still the best. !important');
}, 800);
const render = () => {
t();
};
for(let i = 0; i < 10; i++){ render(); }
var x = render();
var y = render();
x == y;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment