2014-10-04 64 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