Ask Your Question
1

Which version of setTimeout should be used in TypeScript for Node vs Window environment?

asked 2022-08-08 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-06-17 00:00:00 +0000

david gravatar image

In TypeScript, you can use the same version of setTimeout for Node and Window environment. The difference lies in the implementation of the function in each environment.

In Node, the implementation of the setTimeout function is based on the Node event loop and uses a different API than the Window implementation. However, TypeScript provides a compatible API for both environments.

So, you can use the following code for both environments:

setTimeout(() => {
  // your code here
}, delay); 

Note that you will need to import Node's Timer type in your Node project:

import { setTimeout } from  'timers'; 

In a Window environment, the implementation of the setTimeout function will be provided by the browser's JavaScript engine.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-08 11:00:00 +0000

Seen: 9 times

Last updated: Jun 17 '21