I am using Raphael.js to visualize a convex hull algorithm.
However I want to be able to step through the different parts of the code (or use something like sleep()/delay()). However, I can’t see a way of accomplishing this using setTimeOut(). Any ideas?
For example:
sort(points);
//sleep(...)/delay(...)/pause until click?
for(...) {
message('Foo thing');
//sleep(...)/delay(...)/pause until click?
while() {
message('Comparing points');
//sleep(...)/delay(...)/pause until click?
}
}

In JavaScript there is no way to suspend code execution with
sleepfunction. Executing JavaScript code is designed to be non-blocking.Solution with using
debuggerkeyword works on Chrome as well. You just have to open Developer Tools.I prepared demo which works in different way. It simulates
sleepfunction usingsetIntervaland does not block scripts execution. However, it involves some additional code.Let’s assume that we have initial code:
Now, we’d like to rewrite it so that each log shows after one second:
Let me explain it a little bit. Firstly, I define
stepsarray, where I put new functions with bound arguments.bindfunction basically creates new function with arguments which are bound to provided values. More details on MDN page.Example:
In
forloop I create and put new functions usingbind. The last step is to extract these functions fromstepsarray, starting from beginning (usingArray.prototype.shiftmethod), and execute them with interval equal to 1 second.I know it’s not a direct solution of your problem, but I hope it helps you convert your code properly. If you decide to do so, I advise to convert code blocks within
forandwhileloops to functions. It simplifies conversion a little bit.