javascript - Casperjs随机延迟

标签 javascript web-crawler phantomjs casperjs

我使用 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/

相关文章:

JavascriptencodeURIComponent 不编码单引号

seo - 当被拒绝的页面(机器人)仍在 sitemap.xml 中时会发生什么?

Selenium 在 mac 上解释 javascript?

javascript - 使用 R 和 PhantomJS 进行网页抓取交互式 map (javascript)

javascript - jsreport - Openlayers 未加载

javascript - 如何在没有更改属性时添加 CSS 过渡?

javascript - 固定 header 脚本函数 floatHead 正在添加一个额外的 thead

javascript - 未捕获的类型错误 : Cannot use 'in' operator to search for 'length' in true at s jQuery/WordPress

javascript - Google 的爬虫索引异步加载的元素吗?

java - 如何让 PhantomJSDriver 在 selenium 中工作?