Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does setTimeout function runs in background or in parallel?

setTimeout(function(){
     console.log("hello async")
     }, 1000);
console.log("Hello sync");

setTimeout function console the hello async after delays of 1sec, after consoling Hello sync, so does it runs in background or in parallel.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To be clear, setTimeout() is actually window.setTimeout() and window is not part of the Document Object Model or a native object in JavaScript.

When you call setTimeout(), you are accessing a Web API and what happens next is that the browser will (asynchronously) begin a timer and when the time has elapsed, the callback function will be placed into the event queue. The next time the JavaScript runtime is idle, it will check the event queue and execute the next item in that queue.

So, while the callback function is executed by the JavaScript runtime (and thus the single thread of that runtime), the timer itself is handled in a separate thread of the browser.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading