Created
October 30, 2017 05:55
-
-
Save gaganjakhotiya/d171eb35c9ff2985f335f5adc7a6303b to your computer and use it in GitHub Desktop.
Threshold function calls for a set time-period
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
function thresholder(callback, holdMillis, resetOnEachCall) { | |
let timer = null | |
let args | |
return function() { | |
args = arguments | |
if (timer && resetOnEachCall) { | |
clearTimeout(timer) | |
timer = null | |
} | |
if (!timer) { | |
timer = setTimeout(function() { | |
timer = null | |
callback.apply(null, args) | |
}, holdMillis) | |
} | |
} | |
} | |
const thresholdedLog = thresholder( | |
date => console.log(date.getMilliseconds()), | |
10, | |
false | |
) | |
const intervalRefHandle = setInterval(() => thresholdedLog(new Date()), 4) | |
setTimeout(() => clearInterval(intervalRefHandle), 51) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment