Timer Delay Overflow Clamps To One Ms
Node stores timer delays in 32 bits: delays above 2147483647 ms clamp to 1 ms with an overflow warning, so long schedules must re-arm chained timers.
Code
// Node stores timer delays as signed 32-bit integers: a delay above
// 2147483647 ms (about 24.8 days) is clamped to 1 ms, and Node prints
// a TimeoutOverflowWarning — far-future schedules cannot use one timer
const start = Date.now();
setTimeout(() => {
console.log('oversized timer fired after', Date.now() - start, 'ms');
}, 2 ** 31);
// Long durations are built as a chain of short, re-armed timers
function scheduleAfter(durationMs, onDone) {
const step = Math.min(durationMs, 2147483647);
setTimeout(() => {
if (durationMs > step) {
scheduleAfter(durationMs - step, onDone);
} else {
onDone();
}
}, step);
}
scheduleAfter(300, () => console.log('chained schedule completed 300 ms'));Output
Captured from a sandboxed run of this snippet in the build pipeline.
oversized timer fired after 4 ms
chained schedule completed 300 msInside the snippet
The snippet is organized around the function scheduleAfter.
Points of care
- Everything it calls is documented as part of the language core.
Putting it to use
The primary signature is function scheduleAfter(durationMs, onDone)
Expect labeled output — oversized timer fired after, chained schedule completed 300 ms — when the file runs.
Core lines under the microscope
setTimeout(() => {
console.log('oversized timer fired after', Date.now() - start, 'ms');
}, 2 ** 31);
setTimeout(() => {
if (durationMs > step) {
scheduleAfter(durationMs - step, onDone);
} else {
onDone();
}
}, step);
Interactive Tools for This Topic
FAQ
- Is anything beyond the standard library required?
- Nothing beyond the standard library — the snippet is dependency-free. The file defines `scheduleAfter` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.