2012-06-30 88 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將在回調有機會執行之前退出。