Skip to content

setAbortableInterval()

Like setInterval, but when signal aborts, the interval is cleared with clearInterval and callback stops being called. If signal is omitted, behaves like a normal interval (you must clear it yourself).

Examples:

ts
const controller = new AbortController();
setAbortableInterval(() => console.log('tick'), 1000, controller.signal);
// stop: controller.abort();
ts
const ac = new AbortController();
setAbortableInterval(syncStatus, 30_000, ac.signal);
window.addEventListener('beforeunload', () => ac.abort());

Released under the MIT License.