Sunday 15 August 2010

javascript - When to exit when using nested open() functions? -



javascript - When to exit when using nested open() functions? -

i encountered problems in utilize of phantomjs. refer this article. tried nest open() functions, did not results want namely opening 4 urls , printing 4 console.logs.

code :

var page = require('webpage').create(); //新建一个页面 url1 = "-----"; url2 = "-----"; url3 = "-----"; url4 = "http://-----/"; page.open(url1, function(status) { //导航到第一个url console.log('111111111111'); if (status == "fail") phantom.exit(); //如果发生错误,退出程序 page.open(url2, function(status) { //否则在页面加载完成的回调函数中继续导航到第二个url,依次类推 console.log('22222222222222'); if (status == "fail") phantom.exit(); page.open(url3, function(status) { console.log('3333333333333333'); if (status == "fail") phantom.exit(); page.open(url4, function(status) { console.log('444444444444444'); if (status == "fail") phantom.exit(); }); }); }); console.log('close'); phantom.exit(); });

result:

$ phantomjs test.js 111111111111 close

you have exit phantomjs when you're done executing , seems done after open url4:

page.open(url1, function(status) { console.log('111111111111'); if (status == "fail") phantom.exit(); page.open(url2, function(status) { console.log('22222222222222'); if (status == "fail") phantom.exit(); page.open(url3, function(status) { console.log('3333333333333333'); if (status == "fail") phantom.exit(); page.open(url4, function(status) { console.log('444444444444444'); if (status == "fail") phantom.exit(); console.log('close'); phantom.exit(); }); }); }); });

phantomjs asynchronous nicolas says in blog. you're exiting early.

javascript phantomjs

No comments:

Post a Comment