I found some peculiar behavior that only happens in Chrome, and I dont know if this is expected behavior, or if it's a bug in Chrome. I've simplified my code down to this:
function doSequence() {
document.getElementById('test').innerHTML = '1';
alert('2');
document.getElementById('test').innerHTML = '3';
alert('4');
document.getElementById('test').innerHTML = '5';
alert('6');
}
In IE and Edge this works as expected, alternating showing the numbers in the div
and in the alert box, in the proper order (1,2,3,4,5,6).
However, when I run it in Chrome, I see the alert box first, even though the div
is assigned to first. In fact, I see the alert box three times (2,4,6), and the div is always blank. After the alert box closes the third time, I see the number 5 in the div. It acts like it skips assigning '1' and '3' to the div altogether. And then it assigns '5' to the div after the alert has been closed (even though the instruction for assigning '5' is before the alert for '6').
My question is - is this expected behavior with Chrome, or is it a bug? Also, I was able to remedy this by putting the alert
commands inside setTimeout
functions with a delay of around 100ms. Is there another way to work around this behavior?
Here is a live example. It works as you would expect it to in IE and Edge (1,2,3,4,5,6). But if you run it in Chrome you will see the numbers 2,4,6,5.
var doSequence = function() {
document.getElementById('test').innerHTML = '1';
alert('2');
document.getElementById('test').innerHTML = '3';
alert('4');
document.getElementById('test').innerHTML = '5';
alert('6');
}
setTimeout(doSequence,1000);
#test {
font-size:48pt;
padding:14px;
margin:14px;
max-width:48px;
min-height:64px;
border:solid 1px #ccc;
}
<div id='test'>
</div>