SetTimeOut is part of windows object( Browser ) then how it will work with Node js

Clash Royale CLAN TAG#URR8PPP
SetTimeOut is part of windows object( Browser ) then how it will work with Node js
Can anyone explain to me that how browser API's can work with node js?
I searched on stack overflow and found this
Is setTimeout a part of JavaScript it self or it is just an api that the browser provides?
but it didn't explain much about it.
1 Answer
1
Node.js Docs says (Read More),
The functions setTimeout()seems exactly the same since they are available in most browsers, but Node.js actually provides its own implementation of these methods. Timers integrate very closely with the system, and despite the fact that the API mirrors the browser API, there are some differences in implementation.
setTimeout()
setTimeout() can be used to schedule code execution after a designated amount of milliseconds. This function is similar to window.setTimeout() from the browser JavaScript API, however, a string of code cannot be passed to be executed.
setTimeout()
window.setTimeout()
setTimeout() accepts a function to execute as its first argument and the millisecond delay defined as a number as the second argument. Additional arguments may also be included and these will be passed on to the function. Here is an example of that:
setTimeout()
function myFunc(arg)
console.log(`arg was => $arg`);
setTimeout(myFunc, 1500, 'funky');
The above function myFunc() will execute as close to 1500 milliseconds (or 1.5 seconds) as possible due to the call of setTimeout().
myFunc()
setTimeout()
Thanks! So, in short, we have our own implementations for Node Js and I think same is for console.log()
– Nikhil Kapoor
Aug 13 at 5:49
Yaa, global
console instance configured to write to process.stdout and process.stderr, welcome– Suman Kundu
Aug 13 at 5:53
console
process.stdout
process.stderr
Node provides access to a global variable, which refers to the global object. All first level properties are accessible without the global prefix. This includes setTimeout(), setInterval(), console and console.log, apart from others such as process, require() etc.
– KVNam
Aug 13 at 5:59
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Because they decided to add that functionality to the API of node. They did so because you do not have direct access to the event loop, and as of that you are not able to implement such a functionality your self. And they called it the same way, so that it is consisted with an already widely adapted js environment.
– t.niese
Aug 13 at 6:05