CasperJS實踐筆記(3)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.