When the browser, somewhere outside the JavaScript engine, has the event inside the JavaScript engine that want to be notified of, its get placed in the event queue, and WHETHER OR NOT the event have a function response to it.
The event queue gets looked at by JavaScript when the execution stack is EMPTY.
JavaScript engine is always running synchronously, what happening is that the browser is asynchronously putting things into event queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function waitThreeSeconds() { var ms = 3000 + new Date().getTime(); while (new Date() < ms){} console.log('finished function'); } function clickHandler() { console.log('trigger click event!'); } document.addEventListener('click', clickHandler); waitThreeSeconds(); console.log('finished execution'); /* log the result when double click while long run function(line 11) executed: finished function finished execution trigger click event! trigger click event! */ |