javascript - CasperJS 在 for 循环内继续

标签 javascript asynchronous casperjs continue

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/

相关文章:

javascript - Dynamo db Error adding question : { InvalidParameterType: Expected params. Item ['options' ].S 是一个字符串

快速在 UITableView 中异步加载图像 - 顶部单元格没有图片

tomcat - 为什么 Tomcat 的线程比后台线程成本更高?

javascript - CasperJS 未加载页面

Jenkins + CasperJS

javascript - Rangy 函数 highlightSelection 性能

javascript - 一段时间后如何调用 particleJS?

swift - 使用信号量的 XCTest 单异步设置

exe - 在 casperjs 中启动 slimerjs 的正确方法是什么(使用绝对路径)?

javascript - 合并两个数组并创建一个单独的数组