EN
TypeScript - what is correct version when I use setTimeout? (Node.js vs Web Browser)
2
answers
8
points
I have noticed, when I use setTimeout() function in TypeScript, it returns NodeJS.Timeout type.
On MDN we can find:
The returned value is a positive integer value which identifies the timer created by the call.
Should I use it when I write fornt-end application?
I would like to run it later in web browser.
2 answers
5
points
Writing Node.js application use:
const timeout = global.setTimeout(() => { }, 1000); // let timeout: NodeJS.Timeout;
global.clearTimeout(timeout);
Writing web browser application use:
const id = window.setTimeout(() => { }, 1000); // let id: number;
window.clearTimeout(id);
0 comments
Add comment
3
points
Check this one solution:
type Timeout = ReturnType<typeof setTimeout>;
const timeout: Timeout = setTimeout(() => {}, 1000);
0 comments
Add comment