setInterval和casper.js中的this.wait [英] setInterval and this.wait in casper.js

查看:155
本文介绍了setInterval和casper.js中的this.wait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每次迭代之间进行 3次2秒的循环.我尝试了以下3种选择:

I need to make a loop of 3 times and 2 seconds in between each iteration. I tried these 3 options:

选项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();

观察:由于脚本在第一个setInterval被调用之前立即结束,因此什么也没出现.

Observation: Nothing appeared because the script ended right away before the first setInterval being called.

选项2

thenEvaluate()替换为下面的then()

for (i=0; i<3; i++) {
    this.wait(2000);
    this.echo('Using this.wait ' + i);
}

观察:由于this.wait()是异步的,它立即输出3次,然后等待很长时间.这不是我想要的,因为我希望在两者之间有一个延迟.

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 用下面的内容替换then()中的部分.我正在考虑在每次调用wait()之后对waitFunc()进行递归调用.

Option 3 Replace the part in then() with this below. I was thinking about doing a recursive call to waitFunc() after each wait() being called.

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.

所以我的问题是:如何使this.wait或setInterval在这种情况下循环3次?

推荐答案

以下是解决您的问题的示例实现:

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() {});

示例输出:

$ 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.
$

这篇关于setInterval和casper.js中的this.wait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆