I have a while
loop going with an alert()
inside. Inside the while
loop, I want to change the content of an element, in this case with a counter.
I have a snippet which shows the idea.
var test = document.querySelector('div#test');
var limit = 5;
var counter = 0;
do {
counter++;
test.textContent = counter;
console.log(counter)
alert('waiting …');
} while (counter<limit);
div#test {
text-align: center;
}
<div id="test"></div>
For testing purposes, I also include a console.log()
but in the snippet it’s not working like a normal console.
I know that alert()
is very disruptive, and blocks other activity. However, I find that somehow it prevents the element content from being changed, even though that statement is before the alert()
in the loop. However, the console.log()
statement is working, even though you can’t see it in the SO snippet. On a browser, such as Firefox and Safari, the console.log()
prints the new value, but the element content stays unchanged until the end.
I also want the change the CSS style of the element, but I’ve left that off the sample. In any case it doesn’t work either.
I’m curious about what seems to be inconsistent behaviour. What is happening here, and is there a way to allow changes to the element to occur when there’s an alert()
going on?