You have a bug in the code where you set F
and L
timeouts. Your code is equivalent to this:
/* ... */
F(function(){
console.log("callback1");
});
setTimeout(undefined ,5000);
L(function(){
console.log("callback2");
});
setTimeout(undefined, 5000);
/* ... */
Now it should be clear why your program does not behave as you were expecting:
- "Outside all" is not executed first because you are calling
F
andL
before. - The interval is executed last from the same reason.
- The program waits 5 seconds for the two timeouts you set with
undefined
callback.
The easiest way how to fix your code is to add anonymous callback function for setTimeout
calls:
setTimeout(function() { F(function(){
console.log("callback1");
})},5000);
setTimeout(function() { L(function(){
console.log("callback2");
})} , 5000);
Alternatively, you can use bind
to fixate F
and L
parameters (the first parameter of bind
is value for this
):
setTimeout(F.bind(null, (function(){
console.log("callback1");
})),5000);
setTimeout(L.bind(null, (function(){
console.log("callback2");
})) , 5000);