Skip to content

setAbortableTimeout()

Like setTimeout, but if signal aborts before the delay fires, the timer is cleared and callback is never run. If the callback runs normally, the abort listener is removed.

Does nothing special if signal is omitted — behaves like a plain timeout.

Examples:

ts
const controller = new AbortController();
setAbortableTimeout(() => console.log('done'), 500, controller.signal);
// later: controller.abort(); // timeout cancelled, log never runs

Zero-delay scheduling that can still be cancelled before the macrotask runs:

ts
const ac = new AbortController();
setAbortableTimeout(() => startIntro(), 0, ac.signal);
// e.g. on teardown: ac.abort();

Released under the MIT License.