fervid
fervid

Reputation: 2123

Casperjs random delay

I use casperjs and i want to move in site in rantom time intervals. I made such code, but it didn't work:

function getRandomIntFromRange(min, max) {
  return Math.round(Math.random() * (max - min)) + min;
}


var casper = require('casper').create();
casper.start('http://stackoverflow.com/');

casper.on('remote.message', function(msg) {
  this.echo('remote message caught: ' + msg);
});

casper.then(function() { 
  for (i=0; i<=5; i++) { 
    delay = getRandomIntFromRange(1000, 5000);
    this.wait(delay, (
      function(j) { 
        return function() { 
          this.echo('Test ' + j + '; delay: ' + delay); 
        }; 
    })(i)); 
  } 
}); 

casper.run();

Output was so:

Test 0; delay: 1320

Test 1; delay: 1320

Test 2; delay: 1320

Test 3; delay: 1320

Test 4; delay: 1320

Test 5; delay: 1320

Upvotes: 3

Views: 1649

Answers (1)

Adrian May
Adrian May

Reputation: 2182

casper.then(function() { 
  for (i=0; i<=5; i++) { 
    delay = getRandomIntFromRange(1000, 5000);
    this.wait(delay, (
      function(j,d) { 
        return function() { 
          this.echo('Test ' + j + '; delay: ' + d); 
        }; 
    })(i,delay)); 
  } 
}); 

Upvotes: 2

Related Questions