setInterval and this.wait in casper.js

99封情书 提交于 2019-12-03 03:41:16

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.
$
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!