views:

65

answers:

1

I have a large list of instructions that I need executed sequentially but slowly. One every ten milliseconds or so. I'm thinking about a queue type data structure but am unsure how to proceed.

+4  A: 

You probably want to use a timer for this. If you would just put a delay loop in the code, the result would only be that the code takes longer to run, but the final result will show up all at once after the code has finished.

You can use the setTimeout or setInterval methods. Example:

function(){

  var instructions = [
    function() { /* do something */ },
    function() { /* do something */ },
    function() { /* do something */ },
    function() { /* do something */ }
  ];

  var index = 0;

  var handle = window.setInterval(function() {
    if (index < instructions.length) {
      instructions[index++]();
    } else {
      window.clearInterval(handle);
    }
  }, 10);

}();
Guffa