2016-09-19 73 views
0
var casper = require('casper').create(); 
casper.start(); 


casper.then(function(){ 
    for(var i=1;i<3;i++){ 
     this.repeat(3,function(){ 
      this.echo("loop iteration!"); 
     }); 
     this.echo("hello"); 
    } 
}); 

casper.run() 

輸出:如何在CasperJS中進行正確的循環?

你好你好循環迭代!循環迭代!循環迭代!循環迭代!循環迭代!循環迭代!

爲什麼首先打印「你好」?

如何將循環寫入同步循環?

+0

[CasperJS中的then()語句必須包含什麼?如何確定同步/異步函數的執行順序?](http://stackoverflow.com/questions/30105017/what-must-be-wrapped-in-then-statements-in-casperjs-how-to-determine-executio ) –

回答

0

Artjom是正確的。你必須使用then()。例如:

for(var i=1;i<3;i++){ 
     this.repeat(3,function(){ 
      this.echo("loop iteration!"); 
     }); 
     this.then(function(){ 
       this.echo("hello"); 
     }); 
}