javascript casper.js 中的 setInterval 和 this.wait
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13550093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setInterval and this.wait in casper.js
提问by HP.
I need to make a loop of 3 times and 2 seconds in between each iteration. I tried these 3 options:
我需要在每次迭代之间进行3 次 2 秒的循环。我尝试了这 3 个选项:
Option 1
选项1
var casper = require('casper').create({
verbose: false,
logLevel: 'debug'
});
casper.start("http://google.com");
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
})
casper.thenEvaluate(function() {
var x = 0;
var intervalID = setInterval(function () {
console.log("Using setInternal " + x);
if (++x === 3) {
window.clearInterval(intervalID);
}
}, 2000);
});
casper.run();
Observation: Nothing appeared because the script ended right away before the first setInterval
being called.
观察:没有出现,因为脚本在第一个setInterval
被调用之前就结束了。
Option 2
选项 2
Replaced thenEvaluate()
with then()
below
替换thenEvaluate()
为then()
下面
for (i=0; i<3; i++) {
this.wait(2000);
this.echo('Using this.wait ' + i);
}
Observation: It outputs 3 times right away and then a long wait since this.wait()
is async. This is not what I want because I want a delay in between.
观察:它立即输出 3 次,然后等待很长时间,因为它this.wait()
是异步的。这不是我想要的,因为我想要两者之间的延迟。
Option 3Replace the part in then()
with this below. I was thinking about doing a recursive call to waitFunc()
after each wait()
being called.
选项 3用then()
下面的替换部分。我正在考虑waitFunc()
在每次调用后进行递归调用wait()
。
var count = 0;
var waitFunc = function() {
this.wait(2000, function() {
if (count < 3) {
casper.echo('Using this.wait ' + count);
count++;
waitFunc();
}
});
};
Observation: Nothing printed out on the screen.
观察:屏幕上没有打印出任何内容。
So my question is: How to make this.wait or setInterval works in a loop of 3 times like this case?
所以我的问题是:如何使 this.wait 或 setInterval 像这种情况一样在 3 次循环中工作?
回答by NiKo
Here's a sample implementation to solve your problem:
这是解决您的问题的示例实现:
var casper = require('casper').create();
var last, list = [0, 1, 2, 3];
casper.start("http://google.fr/", function() {
this.echo('google');
});
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
casper.thenEvaluate(function() {
window.x = 0;
var intervalID = setInterval(function() {
console.log("Using setInternal " + window.x);
if (++window.x === 3) {
window.clearInterval(intervalID);
}
}, 500);
});
casper.each(list, function(self, i) {
self.wait(500, function() {
last = i;
this.echo('Using this.wait ' + i);
});
});
casper.waitFor(function() {
return last === list[list.length - 1] && 3 === this.getGlobal('x');
}, function() {
this.echo('All done.').exit();
});
casper.run(function() {});
Sample output:
示例输出:
$ casperjs test.js
google
remote message caught: Using setInternal 0
Using this.wait 0
remote message caught: Using setInternal 1
Using this.wait 1
remote message caught: Using setInternal 2
Using this.wait 2
Using this.wait 3
All done.
$