monorepo-run
Version:
Run scripts in monorepo with colors, streaming and separated panes
26 lines (22 loc) • 412 B
JavaScript
exports.throttle = (fn, delay) => {
if (delay <= 0) {
return fn
}
let time = Date.now()
let timeout
let lastArgs
const call = () => {
time = Date.now()
timeout = null
fn(...lastArgs)
}
return (...args) => {
lastArgs = args
const now = Date.now()
if (now - time >= delay) {
call()
} else if (!timeout) {
timeout = setTimeout(call, delay)
}
}
}