码迷,mamicode.com
首页 > Web开发 > 详细

CasperJS实践笔记(3)

时间:2014-09-14 23:27:47      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   使用   strong   for   div   问题   

CasperJS项目中方法的封装和调用问题,第二个遇到的workflow问题。

 

在其他代码中有几个方法,比如fun_A,fun_B,fun_C;

现在某个方法调用它们,有一个错误的示范:

casper.then(function(){

    this.echo("xxxx");

    this.wait(function(){
        this.echo("AAAA");
    })

    this.waitForSelector(‘‘,
        function(){
            fun_a.call(this);
            fun_b.call(this, str1, str2);
            fun_c.call(this);
        }, function(){

        }, 100);

    this.wait(100,function(){
        this.echo("BBBB");    
    })

})

这样调用,预期的workflow是:AAAA --> fun_a --> fun_b --> fun_c --> BBBB。

 

但是实际运行时,发现fun_a、fun_b、fun_c的执行顺序有些意外 ---我们可以认为这是CasperJS的一个bug!

为了避免这个问题,可以这样调用:

casper.then(function(){

    this.echo("xxxx");

    this.wait(function(){
        this.echo("AAAA");
    })

    this.waitForSelector(‘‘,
        function(){
            this.wait(100, fun_a.bind(this));
            this.wait(100, fun_b.bind(this));
            this.wait(100, fun_c.bind(this));
        }, function(){

        }, 100);

    this.wait(100,function(){
        this.echo("BBBB");    
    })

})

当然如果fun_b需要带参数的话,如上一篇文所说,得考虑使用闭包实现方法fun_b。

 

(网络上很少casperjs的实践,很多东西自己摸索,写法也许不够专业,欢迎批评指正),不胜感激之至!

 

CasperJS实践笔记(3)

标签:style   blog   color   io   使用   strong   for   div   问题   

原文地址:http://www.cnblogs.com/andypeker/p/3971857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!