CasperJS继续内部循环

时间:2016-03-15 17:12:25

标签: javascript asynchronous casperjs continue

I have copied this example in CasperJS.
我将如何实施“继续”#39;命令组合?

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++;
    }
});

我在循环中只获得了有效的内容&#39;错误,但......它在一个循环中?

4 个答案:

答案 0 :(得分:2)

所有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++;
    }
});

它以一个额外的缩进级别为代价而缩短了一点。

答案 1 :(得分:1)

它不在循环内。当您使用匿名函数隔离范围时,会丢失&#34;循环范围&#34;引用,所以你的继续不知道循环在哪里。虽然你可以使用闭包(函数范围之外的变量),但你不能引用外部循环...

在控制台中查看:

for (var i=0;i<10;i++) continue;

时,

就像魅力一样

for (var i=0;i<10;i++) (function(){ continue; })();

不起作用,因为循环引用丢失。

如何处理?只需返回函数的结果,例如

for (var i=0;i<10;i++) {
  if ((function(){ return i === 3; })()) {
    console.log('continued'); 
    continue;
  }
}

答案 2 :(得分:1)

Once you are inside an inner function (you actually have two levels, the IIFE around the body of the loop and the function you're passing to thenOpen), you're no longer in the scope of the loop.

Also, the functions that are passed to casper.then and casper.thenOpen execute asynchronously (but in order). If you want to affect the order, you have to check values from within those functions.

casper.then(function() {
    var current = 1;
    var end = 4;
    var something = true;
    var shouldContinue = false;

    for (;current < end;) {

      (function(cntr) {
        casper.thenOpen('about:blank', function() {
          shouldContinue = false;
          this.echo('loop number: '+ cntr);    
        });
      })(current, s);

      (function(cntr) {
        casper.then(function(){
          if (something) {
            shouldContinue = true;
          }
        });
      })(current);


      (function(cntr) {
        if (shouldContinue) {
            return;
        }
        casper.then(function(){
          this.echo('this still gets printed');
        });
      })(current);

      current++;
    }
});

答案 3 :(得分:1)

此解决方案有效,但我对此并不满意。为每个带有全局跳过变量的casper.then部分添加了if / else。一旦满足继续条件,所有后续的casper.thens都被有效地跳过。跳过在每个循环开始时重置。

get()