I've done the research, so I appreciate that there's no sleep() method in javascript. What I can't figure out is what is the correct alternative for me, because setTimeout() isn't solving my situation due to its asynchronous nature.
My program parses input from users and executes it in an eval() statement. The executing code will call functions that I've pre-written and made available to the users. These functions include draw(color), which will place a colored square on the screen at a spot defined by the user. It's not necessarily relevant for the question, but in case you want to know I'm just making a <div>
and inserting a semi-transparent <img>
with the proper heights.
Now here's where I want the sleep function: If the user draws many squares, javascript displays them all at once, but I want to display them one after the next, with a delay I can control. This cannot be done with setTimeout(). Trying to self-write a delay function calculating start and end times will just freeze up the browser, and believe me I've tried it and it still draws everything at once after being frozen.
My instinct is to put a sleep() call as the last line of my draw() method, so each square the user creates in their code will take e.g. 200 ms to render, but no such function exists.
Oh JavaScript gurus, how can I get this kind of functionality in a web page if javascript won't sleep?
ideally:
function draw() {
var easel = document.getElementById("canvas");
var square = document.createElement("div");
square.setAttribute("...");
easel.appendChild(square);
window.sleep(200);
}