Using temporal:

var temporal = require("temporal");
var startIndex = 0;
var endIndex = 10;
var incrementIndex = 1;
var delay = 100;
var array = [1, 2, 3, 4, 5];
var tasks = [];


while ( startIndex < endIndex ) {
  for ( var i = 0; i < array.length; i++ ) {
    tasks.push({
      delay: delay,
      task: function() {
        console.log(i, Date.now());
        // do stuff
      }
    });
  }

  startIndex = startIndex + incrementIndex;

  tasks.push({
    delay: delay,
    task: function() {
      console.log(startIndex, Date.now());
      // do stuff
    }
  })
}


temporal.queue(tasks);

// Output:


5 1420338358098
5 1420338358189
5 1420338358289
5 1420338358389
5 1420338358489
10 1420338358589
5 1420338358689
5 1420338358789
5 1420338358889
5 1420338358989
5 1420338359089
10 1420338359189
5 1420338359289
5 1420338359389
5 1420338359489
5 1420338359589
5 1420338359689
10 1420338359789
5 1420338359889
5 1420338359989
5 1420338360089
5 1420338360189
5 1420338360289
10 1420338360389
5 1420338360489
5 1420338360589
5 1420338360689
5 1420338360789
5 1420338360889
10 1420338360989
5 1420338361089
5 1420338361189
5 1420338361289
5 1420338361389
5 1420338361489
10 1420338361589
5 1420338361689
5 1420338361789
5 1420338361889
5 1420338361989
5 1420338362089
10 1420338362189
5 1420338362290
5 1420338362390
5 1420338362490
5 1420338362590
5 1420338362690
10 1420338362790
5 1420338362890
5 1420338362990
5 1420338363090
5 1420338363190
5 1420338363290
10 1420338363390
5 1420338363490
5 1420338363590
5 1420338363690
5 1420338363790
5 1420338363890
10 1420338363990



There is a 1 ms drift, but aside from that all tasks occurred exactly 100ms
apart.





On Sat Jan 03 2015 at 9:09:52 PM schone <[email protected]> wrote:

> I'm trying to wait or sleep a fixed amount of time inside a while and/or
> for loop in node.js (I'm trying to mimic delayed random data). As an
> example, I have the following structure:
>
> var startIndex = 0,
>     endIndex = 10,
>     incrementIndex = 1;while ( startIndex < endIndex ) {
>
>   for ( var i = 0; i < array.length; i++ ) {
>     // do stuff
>     // sleep here?
>   }
>   // do stuff
>   startIndex = startIndex + incrementIndex;
>   // sleep here?}
>
> Has anyone got any tips for how you would implement a setTimeout or
> similar function in node.js with the above structure?
>
> Thank you!
>
>  --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/cf919f6d-17a1-43a4-980c-fb525e48bd10%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/cf919f6d-17a1-43a4-980c-fb525e48bd10%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAHfnhfpqSCAn4R96yKC%3D_B5Vzb1shhYidAQ-7T8B8n9QKmrw5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to