I have copied this example in CasperJS.
我将如何组合实现“继续”命令?
casper.then(function() {
var current = 1;
var end = 4;
var something = true;
for (;current < end;) {
(function(cntr) {
casper.thenOpen('about:blank', function() {
this.echo('loop number: '+ cntr);
});
casper.then(function(){
if (something) {
continue; // ERROR: 'continue' is only valid inside a loop statement
}
});
casper.then(function(){
this.echo('this still gets printed');
});
})(current);
current++;
}
});
我收到“仅在循环内有效”错误,但是...它在循环内?
最佳答案
所有 casper.then*
和 casper.wait*
函数都是异步的。您传递给它们的步骤函数实际上是在循环完成后执行的。因此,您不能使用 continue
跳转到下一个迭代。
但是,您可以嵌套所有步骤函数,从而实现您的预期:
casper.then(function() {
var current = 1;
var end = 4;
var something = true;
while(current < end) {
(function(cntr) {
casper.thenOpen('about:blank', function() {
this.echo('loop number: '+ cntr);
});
casper.then(function(){
if (something) {
return; // this replaced `continue`
}
// more synchronous code
this.then(function(){
this.echo('this still gets printed');
});
});
})(current);
current++;
}
});
它的长度要短一些,但代价是多了一层缩进。
关于javascript - CasperJS 在 for 循环内继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36017827/