Created
May 1, 2020 15:18
-
-
Save Tahseenm/729c3bfdc1981f041137b94fa0661c3a 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
/* :: (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