The setTimeout
function in TypeScript is used to delay the execution of a function by a specified number of milliseconds.
Here are three solutions to write the setTimeout
type in TypeScript. Replace <ms>
with the number of milliseconds, you want to delay the execution.
Solution #1
let timer: NodeJS.Timer;
timer = setTimeout(() => { }, <ms>)
...
clearTimeout(timer);
Solution #2
type TimerType = ReturnType<typeof setInterval>;
let timer: TimerType;
timer = setTimeout(() => { }, <ms>)
...
clearTimeout(timer);
Solution #3
let timer: ReturnType<typeof setInterval>;
timer = setTimeout(() => { }, <ms>)
...
clearTimeout(timer);