Created
March 14, 2018 17:52
-
-
Save AnatoliyLitinskiy/f2aca61f5072fc2c1b30600bd76ace75 to your computer and use it in GitHub Desktop.
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 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