gpt4 book ai didi

node.js - NodejS + PhantomJS 警告 : exit() was called before waiting for commands to finish. 确保您没有过早调用 exit()

转载 作者:搜寻专家 更新时间:2023-10-31 23:40:41 24 4
gpt4 key购买 nike

尝试将 PhantomJS 与 NodeJS 结合使用来检查网站加载时间。

但是之后收到了这个消息

phantom_instance.exit();

已经尝试在 Promise 返回后运行这一行

这是我的代码:

function timer(url) {
return new Promise(function(resolve, reject) {
let phantom_instance = null;
let sitepage = null;
let start_time = null;
let end_time = null;
let interval_obj = null;
let loading_time = 0;
phantom.create()
.then(function(instance) {
phantom_instance = instance;
return phantom_instance.createPage();
})
.then(function(page) {
sitepage = page;
page.property('viewportSize', {
width: 1280,
height: 1024
})
start_time = Date.now();
return page.open(url);
})
.then(function(status) {
return new Promise(function(resolve, reject) {
if (status == 'success') {
interval_obj = setInterval(function() {
//tell the browser to return document.readyStatue
sitepage.evaluate(function() {
return document.readyState;
})
.then(function(ready_state) {
if (ready_state == "complete") {
end_time = Date.now();
sitepage.close();
clearInterval(interval_obj);
loading_time = end_time - start_time;
return resolve(loading_time);
} else {
console.log(ready_state);
}
})
}, 1000);
} else {
//phantom_instance.exit();
return reject(status);
}
});
})
.then(function(loading_time) {
console.log("[Speed Tester] Completed in : " + loading_time);
phantom_instance.exit();
return resolve({time:loading_time});
})
});
}

有什么想法吗?

谢谢。

最佳答案

我可以通过执行 var promise = page.close(); 来避免提到的警告并在执行 phantom_instance.exit() 之前等待 promise 解决。

关于node.js - NodejS + PhantomJS 警告 : exit() was called before waiting for commands to finish. 确保您没有过早调用 exit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com