Skip to content

Instantly share code, notes, and snippets.

@gaganjakhotiya
Created October 30, 2017 05:55
Show Gist options
  • Save gaganjakhotiya/d171eb35c9ff2985f335f5adc7a6303b to your computer and use it in GitHub Desktop.
Save gaganjakhotiya/d171eb35c9ff2985f335f5adc7a6303b to your computer and use it in GitHub Desktop.
Threshold function calls for a set time-period
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