Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Created April 15, 2018 01:07
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 karenpeng/c339bfedb1da29607053db0d249a7fd8 to your computer and use it in GitHub Desktop.
Save karenpeng/c339bfedb1da29607053db0d249a7fd8 to your computer and use it in GitHub Desktop.
const throttle = (func, delay) => {
let executable = true;
return () => {
if (executable) {
func();
executable = false;
setTimeout(() => {
executable = true;
}, delay);
}
};
}
const debounce = (func, delay) => {
let lastTimeStamp = Infinity;
return () => {
const now = Date.now();
if (lastTimeStamp - now > delay) {
func();
}
lastTimeStamp = now;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment