'wait' or 'sleep' with loops in node.js

6,141 views
Skip to first unread message

schone

unread,
Jan 3, 2015, 11:25:18 AM1/3/15
to nod...@googlegroups.com

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!


Rick Waldron

unread,
Jan 3, 2015, 10:01:00 PM1/3/15
to nod...@googlegroups.com
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. 





--
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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cf919f6d-17a1-43a4-980c-fb525e48bd10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Schmidt

unread,
Jan 4, 2015, 9:25:23 PM1/4/15
to nod...@googlegroups.com
You are trying to write synchronous code. Don't. Write asynchronous code. Use the built-in JavaScript setTimeout function, etc. If you are trying to simulate data arriving from an external service after an unpredictable delay, that data will certainly arrive asynchronously as well. Your current code tries to process all the data at once. Rewrite your code so that you have a self-contained function that can process a single unit of work, then arrange to call that function whenever the data arrives (or is simulated to arrive).



Reply all
Reply to author
Forward
0 new messages