Languages

TypeScript - what is correct version when I use setTimeout? (Node.js vs Web Browser)

8 points
Asked by:
Dexter
660

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
Answered by:
Dexter
660

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
Answered by:
Dexter
660

Check this one solution:

type Timeout = ReturnType<typeof setTimeout>;

const timeout: Timeout = setTimeout(() => {}, 1000);
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join