I've read a lot of articles about JavaScript single-threaded execution model, event-loop and event queue.
One thing is not clear though. I created a fiddle to illustrate my question: http://jsfiddle.net/yzpmf67f/
<button onClick="window.compute()">Do computation</button>
<button onClick="alert('doing')">Do something else</button>
window.compute = function compute() {
var result = 0;
for (var i = 0; i < 100000; i++) {
for (var j = 0; j < 100000; j++) {
result = i + j;
}
};
var textnode = document.createTextNode(result);
document.body.appendChild(textnode);
}
If you click on "Do computation" button it will take some time and block the page. But if you click on "Do something else" button, while the page is still hanging, it will still be added to the queue and executed after our computation has finished.
So if JS is single-threaded who is putting UI events into event queue while we are computing something else?