Implement setCancellableInterval(callback, delay, ...args). It works like setInterval, but returns a cancel function instead of an interval id. After the cancel function is called, callback never runs again.
let ticks = 0;
const cancel = setCancellableInterval(() => {
ticks++;
if (ticks === 3) cancel(); // can cancel from inside the callback
}, 100);
// ticks reaches 3 after ~300ms and then stops
callback(...args) every delay ms until cancelled. delay defaults to 0.setInterval if the callback takes longer than delay?Spotted a mistake or a rights concern? Report an issue