-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextendedSetTimeout.js
22 lines (17 loc) · 990 Bytes
/
extendedSetTimeout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* This method comes handy when you need to create time outs with a duration bigger than the maximum allowed
* duration for the NodeJS in-built method setTimeout (2147483647 = (2^31)-1)
*
* @author 5antos#4876
* @param {function} callback Function to run when the timer ends
* @param {number} ms Time value in milliseconds (timer)
* @returns {void}
*/
function extendedSetTimeout(callback, ms) {
const biggestInt = (2**31)-1
const max = ms > biggestInt ? biggestInt : ms
setTimeout(() => ms > max ? extendedSetTimeout(callback, ms - max) : callback(), max)
}
// Example Outputs (extendedSetTimeout VS setTimeout):
extendedSetTimeout(() => console.log('5 years later!'), 1.5778463e11) // [👍] Outputs '5 years later!' after 5 years... if your application remains online the entire time
setTimeout(() => console.log('5 years later?'), 1.5778463e11) // [👎] Outputs a warning because the provided duration exceeds the maximum allowed duration for this method