2014-10-04 81 views
3

我使用casperjs,我想要在网站中以rantom时间间隔移动。 我做了这样的代码,但没有奏效:Casperjs随机延迟

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(); 

输出是这样:

测试0;延迟:1320

测试1;延迟:1320

测试2;延迟:1320

测试3;延迟:1320

测试4;延迟:1320

测试5;延迟:1320

+0

你还没说什么没有奏效。请通过编辑您的问题来澄清您的预期结果。 – 2014-10-05 19:59:27

回答

2
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)); 
    } 
}); 
+0

固定......... – 2014-10-06 08:53:38

+0

为我工作:) – 2015-12-02 08:59:13