The setInterval()
method calls a function at specified intervals (in milliseconds). To cancel an interval, use the id
returned from setInterval()
method.
Here are three solutions to write the setInterval
type in TypeScript. Replace <ms>
with the number of milliseconds, you want to repeat the execution.
Solution #1
let timer: NodeJS.Timer;
timer = setInterval(() => { }, <ms>)
...
clearInterval(timer);
Solution #2
type TimerType = ReturnType<typeof setInterval>;
let timer: TimerType;
timer = setInterval(() => { }, <ms>)
...
clearInterval(timer);
Solution #3
let timer: ReturnType<typeof setInterval>;
timer = setInterval(() => { }, <ms>)
...
clearInterval(timer);