javascript - PhantomJS 5-min Wiki phantom.exit() 奇怪

标签 javascript phantomjs

所以来自 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/

相关文章:

JavaScript (JQuery/Ajax) 计时器自行停止

javascript - 关闭弹出窗口时暂停当前视频

javascript - 通过angularjs在mongodb中插入嵌入文档

c# - 如何防止 phantomjs webdriver 耗尽我的所有端口

javascript - Meteor Spiderable 软件包不适用于 Meteor 0.9.0

javascript - 无法使用 Phantomjs 代码示例在 instagram.com 上截图,为什么总是黑屏?

javascript - 从 Canvas 保存图像时缺少扩展名

javascript - 我需要帮助访问对象的属性

javascript - 使用 R 将字段添加到在线表单并抓取生成的 javascript 创建的表

javascript - 如何通过 PhantomJS 触发 MouseDown?