I have a while loop calling a function every second:
function draw (){
console.log(then-now)
};
var i = 0
while(i < 100){
setTimeout(draw,1000)
i++;
};
But this is not waiting a second between each call to draw, it is calling it 100 times at once.
Try
setInterval:If you wanted to use
setTimeout, you’d have to set the next iteration from inside thedrawfunction itself.This is because
setTimeoutis asynchronous, so your while loop doesn’t pause — it keeps going, launching each instance ofdrawalmost instantaneously.