Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why setTimeout works on Node JS?

I am hearing that setTimeout(), the Web API is not included in JavaScript quite often. So, if it is only Web API it is natural to think as it won't work anywhere except in the browser. So, why setTimeout is working in my terminal when I just run a plain old JS file with the node ${filename} command?

like image 246
Ahmad Shahbalayev Avatar asked Jun 04 '26 21:06

Ahmad Shahbalayev


1 Answers

The browser and node.js both have a global setTimeout function, but the return types are different between the two.

  • node.js setTimeout returns a Timeout object: https://nodejs.org/api/timers.html#settimeoutcallback-delay-args
  • Browser setTimeout returns an integer timer id: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout
like image 58
dlannoye Avatar answered Jun 07 '26 10:06

dlannoye