Created
February 3, 2017 20:11
-
-
Save gtwalford/f1dd58f4e6bb90b31aee86df82cca880 to your computer and use it in GitHub Desktop.
Throttle / Debounce example
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
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