So i came up with this example and i cant understand why the setTimeout with 0 seconds is the last one to be executed
function waitThreeSeconds() {
setTimeout(function() {
console.log("Finished Function");
}, 0);
}
function clickHandler() {
console.log("Clicked");
}
document.addEventListener('click', clickHandler);
waitThreeSeconds();
//waiting 5 seconds
var ms = 5000 + new Date().getTime();
while (new Date() < ms) {}
console.log('Finished Execution');
If its true that the setTimeouts callback is added to the queue why is it that everytime i do a click event it is added to the queue earlier then the callback for the setTimeout. That is until the Global Execution Context ('main') is popped off the stack