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:
xxxxxxxxxx
1
const timeout = global.setTimeout(() => { }, 1000); // let timeout: NodeJS.Timeout;
2
3
global.clearTimeout(timeout);
Writing web browser application use:
xxxxxxxxxx
1
const id = window.setTimeout(() => { }, 1000); // let id: number;
2
3
window.clearTimeout(id);
0 commentsShow commentsAdd comment
3 points
Check this one solution:
xxxxxxxxxx
1
type Timeout = ReturnType<typeof setTimeout>;
2
3
const timeout: Timeout = setTimeout(() => {}, 1000);
0 commentsAdd comment