I am creating a course for people who have never done programming, and would like to demonstrate basic DOM-manipulation without using async programming or callback functions.
I thought I could do something like this:
function color(element, color) {
element.style = "background: " + color + ";";
}
function wait(milliseconds)
{
var e = new Date().getTime() + (milliseconds);
while (new Date().getTime() <= e) {}
}
function disco() {
var body = document.getElementById("body")
color(body, "red")
wait(500)
color(body, "darkGreen")
wait(500)
color(body, "darkBlue")
}
disco()
This runs fine, but the UI doesn't refresh until the end, so the background never turns red or green - it just ends up blue.
Is there any way to force a repaint during the execution of the function?
I am aware that this not a good idea or even acceptable for an actual implementation, but my aim is to keep things comprihensible to newbies.