For my upcoming course catered towards beginners with no programming experience, I want to demonstrate basic DOM-manipulation without involving async programming or callback functions.
I came up with the following idea:
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()
While this script runs smoothly, the UI fails to update until the function completes, resulting in a blue background without showing red or green. Is there any way to trigger a repaint during the function execution?
I understand that this approach is not ideal for practical use, but my goal is to make it easy for newcomers to understand.