0

I am using setTimeout function:-

function myFunction() {
 alert("1"); // Called 1st


setTimeout(function(){ 
     alert("2");  // Called Third
}, 0000);

/*Same as setTimeout window.setTimeout(slowAlert, 0000);function slowAlert() { alert("That was Same as setTimeout!");}*/ 

     alert("39"); // Called Second
} 

I am unable to understand why alert('2') calls on third time even I am using zero seconds

VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
  • Because `setTimeout` is asynchronous function. – Sankar Dec 01 '16 at 06:37
  • But Time is zero seconds so it won't matter whether setTimeout function is asynchronous because it don't use any second for executing that codes of line – VIKAS KOHLI Dec 01 '16 at 06:39
  • 1
    @VIKASKOHLI it does matter obviously, or you wouldn't have to ask the question :) – xShirase Dec 01 '16 at 06:44
  • @VIKASKOHLI, _But Time is zero seconds so it won't matter whether setTimeout_ - [see my answer](http://stackoverflow.com/a/40904193/2545680). The time specifies when the callback should be added to the event loop, not when the callback will be executed. – Max Koretskyi Dec 01 '16 at 06:49
  • Does this answer your question? [Why doesn't setTimeout(.., 0) execute immediately?](https://stackoverflow.com/questions/36904773/why-doesnt-settimeout-0-execute-immediately) – Heretic Monkey Dec 09 '21 at 13:47

5 Answers5

3

setTimeout adds your callback to the event loop, which will be called later when a browser isn't busy. The second parameter just tells a browser when your callback will be added to the event loop, not when execute it. In your case it's zero, so the callback is added almost immediately (it's actually in about 4 milliseconds) to the loop, but it will be executed later when a browser has time. The other alerts in your code don't use setTimeout and so they are executed immediately in current tick, that's why they are executed before the callback.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • It means even we are using zero seconds, the setTimeout function itself take some time – VIKAS KOHLI Dec 01 '16 at 06:51
  • 1
    @VIKASKOHLI, I didn't understand what you mean. If you use zero seconds, it means that callback will be added to the the loop immediately, but it will be executed later when a browser has time. You need to understand how the even loop works. – Max Koretskyi Dec 01 '16 at 06:56
1

setTimeout() calls callback function asynchronously. So there is no guarantee of order even though it is set with zero timeout.

To make it synchronous, remove setTimeout() and just call alert('2'); directly.

function myFunction() {
   alert("1"); // Called 1st
   alert("2");  // Called second
   alert("39"); // Called third
} 

Update:

If you want to make sure that the order remain intact then move alert("39") also inside setTimeout().

function myFunction() {
   alert("1"); // Called 1st
   setTimeout(function() {
       alert("2");  // Called second
       alert("39"); // Called third
   }, 0);
}
Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • Is it possible to execute that code on 2nd time using setTimeout Function because I want to run that codes of line on second time if there is no delay – VIKAS KOHLI Dec 01 '16 at 06:44
1

Ok, so, let's work a tick at a time and see what your app is doing :

Tick 1 - alert("1"); An alert, better display it now!

Tick 2 - setTimeout(function(){ },0000); A timeout with 0seconds? OK, I'll wait

Tick 3 - alert("39"); Another Alert, display!!

Tick 4 - alert("2"); It's been 0 seconds, what was in that timeout again?

xShirase
  • 11,975
  • 4
  • 53
  • 85
0

since JS in browser is single threaded setTimeout creates a new "stack" that gets executed n milliseconds after the current stack is cleared, i.e. after the current function finished

nottu
  • 369
  • 1
  • 12
0

Because setTimeout is asynchronous kind of function. Because it breaks the synchronous execution flow. But it does not executed simultaneously like a separate thread.

setTimeout(function(){ 
     alert("2");  // Called Third
}, 0000);

The above code executed once after all other statements outside of it are finished executing.

Sankar
  • 6,908
  • 2
  • 30
  • 53