Skip to content

Instantly share code, notes, and snippets.

@AnatoliyLitinskiy
Created March 14, 2018 17:52
Show Gist options
  • Save AnatoliyLitinskiy/f2aca61f5072fc2c1b30600bd76ace75 to your computer and use it in GitHub Desktop.
Save AnatoliyLitinskiy/f2aca61f5072fc2c1b30600bd76ace75 to your computer and use it in GitHub Desktop.
const allowEvery = (f, delay = 300) => {
let timeOut = null;
let lastCall = null;
return (...args) => {
const n = Date.now();
if (lastCall === null) {
lastCall = n;
} else if (n - lastCall > delay) {
lastCall = n;
f(...args);
return;
}
if (timeOut) clearTimeout(timeOut);
timeOut = setTimeout(() => {
timeOut = null;
lastCall = Date.now();
f(...args);
}, delay - (n - lastCall)); // .unref();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment