Skip to content

Instantly share code, notes, and snippets.

@Tahseenm
Created May 1, 2020 15:18
Show Gist options
  • Save Tahseenm/729c3bfdc1981f041137b94fa0661c3a to your computer and use it in GitHub Desktop.
Save Tahseenm/729c3bfdc1981f041137b94fa0661c3a to your computer and use it in GitHub Desktop.
/* :: (Function | string, number | bigint, ...any) -> Function */
const setBigTimeout = (fn, delay, ...args) => {
const maxSafeDelay = typeof delay === 'bigint'
? BigInt(2147483647)
: 2147483647
let id
const cancel = () => {
clearTimeout(id)
}
const run = currDelay => {
if (currDelay <= maxSafeDelay) {
id = setTimeout(fn, currDelay, ...args)
}
const remaining = currDelay - maxSafeDelay
id = setTimeout(() => run(remaining), maxSafeDelay)
}
run(delay)
return cancel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment