Suppose I have the following code in javascript, which interacts with a HTML canvas:
function draw(){
canvas.width = canvas.width
canvas.style.backgroundColor = 'red'
alert('drawn')
}
function wait(){
alert('waiting')
while (true){
}
}
function call(){
draw()
wait()
}
and then I call the function call()
. What I'd expect to happen is that the function should call draw()
, turning the canvas red, and then calling wait()
and stalling. And indeed, the window alerts "drawn" and "waiting" as expected. However, the canvas doesn't turn red until the function wait()
ends. (which, in this case will never happen). Why does the canvas lag like this, and, supposing that my wait()
function were instead some function that takes a few seconds to execute, how can I get the canvas to fully update in the draw()
function before going on to the wait()
function?