I would like to change the HTML of a node, then wait 500ms, then continue my code where it has to change again. like this:
centerDiv.innerHTML = "waiting";
pauseMs(interTrialInterval);
...
centerDiv.innerHTML = "going again"
where pauseMs is as follows:
function pauseMs(ms) {
ms += new Date().getTime();
while (new Date() < ms){}
}
When I execute the code, the 'waiting' doesn't show up, as a matter of fact, digging into it I have the feeling that the HTML gets updated after the pauseMs is completed, and a few lines later in the code gets changed to "going again".
Strange thing: If I debug it and step over the code, it works as it should.
I tried forcing a repaint by adding offsetHeigth, that didn't work.
centerDiv.innerHTML = "waiting";
centerDiv.offsetHeight;
Any help most appreciated, Joris
EDIT: As pointed out by many, this subject has been extensively discussed in this SO question. However one must notice that DOM changes requested before this "busy pause" are NOT executed by the browser until the pause is over.
As suggested I refactored my code using setTimeout.