如何在CasperJS中设置wait()的值?

时间:2015-11-11 22:27:57

标签: javascript phantomjs wait casperjs

这是我的完整代码..我想要的是casper.wait的随机等待时间为1-3秒。如果我把“casper.wait(1000,function(){”输入一个数值,如果它工作,但是casper.wait(time,function(){输入变量值不起作用。

casper.then(function() {

  this.echo('Looking random number.....');
  rrandom = Math.round(Math.random() * 3);

  if (rrandom == 1) {
    time = 1000
  }
  if (rrandom == 2) {
    time = 2000
  }
  if (rrandom == 3) {
    time = 3000
  }
});

casper.wait(time, function() {
  this.echo('result');
});


casper.run();

1 个答案:

答案 0 :(得分:1)

在您的示例 rrandom 中,有时会等于0,因为Math.round()舍入< 0.49为零。因此, 时间 有时会被定义,从而破坏了脚本。

我会建议这样的事情:

var time;
casper.then(function() {
  var maxSecTimeout = 3;

  this.echo('Pausing for ' + maxSecTimeout + ' seconds');

  time = Math.ceil(Math.random() * maxSecTimeout) * 1000;  
});

casper.wait(time, function() {
  this.echo('result');
});

casper.run();