所以来自 wiki 的示例在此处的两个位置有 phantom.exit()
。为什么我不能将 phantom.exit()
放在脚本的末尾?这对我来说意义不大。
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?
最佳答案
page.open
方法是异步的,所以你传递给它的回调函数会在未来的某个时间运行(当 address
引用的资源有完成加载)。
如果您在该脚本的末尾调用 phantom.exit()
,PhantomJS 将在回调有机会执行之前退出。
关于javascript - PhantomJS 5-min Wiki phantom.exit() 奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277653/