Implement setCancellableInterval(callback, delay, ...args). It calls callback(...args) every delay milliseconds. It returns a cancel function.
- The first call happens after
delayms, not right away. - Calling cancel stops all future calls. Calling it again does nothing.
- After cancel, no timer is left waiting to run.
- Each call to
setCancellableIntervalis independent. Cancelling one doesn't affect another.
Input: const cancel = setCancellableInterval(tick, 100);
// 350 ms pass
cancel();
Output: tick was called 3 times (at 100, 200 and 300 ms)
Input: setCancellableInterval(log, 50, 'a', 1);
Output: log('a', 1) every 50 ms