There are two approaches to executing code block by block, rather than at full speed. The most powerful and flexible approach is to use the JS-Interpreter. However its interface to your API isn't the simplest thing to configure. The other approach is to record a log, then play it back slowly. Let's look at this second approach.
Your blocks generate code that looks like this:
moveUp();
moveLeft();
Next write functions that simply log these movements, don't actually perform animations:
var log = [];
function moveUp() {
log.push('UP');
}
function moveLeft() { log.push('LEFT');
}
Once the user's program is fully executed, then step through the log, with a delay between each step:
function step() {
var action = log.shift();
if (!action) return;
if (action === 'UP') {
avatar.style.left = (parseInt(avatar.style.left) - 10) + 'px';
} else if (action === 'LEFT')
avatar.style.left = (parseInt(avatar.style.top) - 10) + 'px';
}
// Wait 1000 ms, then execute the next step.
setTimeout(step, 1000);
}
step();