My problem is more complex, but I've arrived at this simple & concise example:
HTML:
First: <span id="first"></span> <br/>
Then: <span id="second"></span> <br/>
And then: <span id="third"></span> <br/>
And then: <span id="fourth"></span> <br/>
And then: <span id="fifth"></span>
JS (+jQuery (if matters)):
$(document).ready(function() {
doStuff();
});
function doStuff() {
$("#first").text(new Date().getSeconds());
var i = 1;
while (i < 1000000000) {
i++;
}
$("#second").text(new Date().getSeconds());
var i = 1;
while (i < 1000000000) {
i++;
}
$("#third").text(new Date().getSeconds());
var i = 1;
while (i < 1000000000) {
i++;
}
$("#fourth").text(new Date().getSeconds());
var i = 1;
while (i < 1000000000) {
i++;
}
$("#fifth").text(new Date().getSeconds());
}
And fiddle.
From this example, my intuition tells me the $("#first")
should receive its text value way before the $("#fifth")
. However, they all appear at the same time. And no, it's not a matter of JS running really fast, as you can see in the example that the $("#first")
has its value ~2 seconds lower than the $("#fifth")
.
Why is this happening (and what!), and what solution(s) exist?