2012-06-30 46 views
3

所以维基的例子在这里有两个地方有phantom.exit()。为什么我不能在脚本的末尾放置phantom.exit()?这对我来说没有太大的意义。PhantomJS 5分钟Wiki phantom.exit()oddity

var page = require('webpage').create(), 
t, address; 

if (phantom.args.length === 0) { 
    console.log('Usage: loadspeed.js <some URL>'); 
    phantom.exit(); //Why does this only work if "phantom.exit()" is here, 
} else { 
    t = Date.now(); 
    address = phantom.args[0]; 
    page.open(address, function (status) { 
     if (status !== 'success') { 
      console.log('FAIL to load the address'); 
     } else { 
      t = Date.now() - t; 
      console.log('Loading time ' + t + ' msec'); 
     } 
     phantom.exit(); //and here. 
    }); 
} 
// but not just a single one here? 

回答

6

page.open方法是异步的,所以你传递给它的回调函数将在未来的某个时刻运行(当资源提到了address完成加载)。

如果您在该脚本的末尾拨打phantom.exit(),PhantomJS将在回调有机会执行之前退出。