我使用 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
最佳答案
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));
}
});
关于javascript - Casperjs随机延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26192970/