我有一个用phantomjs-node实现的'waitfor‘,看起来sitepage.evaluate在计算为真的时候有一个很大的延迟。您将在下面看到,我正在注销内容值和内容日志,内容日志应该评估为真,但这似乎不会在事件发生后的10秒左右发生。
你知道是什么原因导致这种延迟吗?或者有没有更好的方法来评估?
let Promise = require('bluebird');
let phantom = require('phantom');
let sitepage;
let phInstance;
phantom.create()
.then(instance => {
phInstance = instance;
return instance.createPage();
})
.then(page => {
sitepage = page;
return page.open('https://thepiratebay.org/search/game/0/99/0');
})
.then(status => {
return waitUntil(function() {
//This returns the correct content after a short period, while the evaluate ends up taking maybe 10s longer, after this content should evaluate true.
sitepage.property('content').then(content => {
console.log(content);
});
return sitepage.evaluate(function() {
return document.getElementById('searchResult');
});
}).then(function() {
return sitepage.property('content');
}).catch(Promise.TimeoutError, function(e) {
sitepage.close();
phInstance.exit();
});
})
.then(content => {
console.log('content');
console.log(content);
sitepage.close();
phInstance.exit();
})
.catch(error => {
console.log(error);
phInstance.exit();
});
var waitUntil = (asyncTest) => {
return new Promise(function(resolve, reject) {
function wait() {
console.log('--waiting--');
asyncTest().then(function(value) {
if (value) {
console.log('resolve');
resolve();
} else {
setTimeout(wait, 500);
}
}).catch(function(e) {
console.log('Error found. Rejecting.', e);
reject();
});
}
wait();
});
}发布于 2016-11-03 04:05:09
据我所知,函数sitepage.evaluate返回元素。根据我的经验,这不是一个好主意。你最好从sitepage.evaluate返回一个短字符串。例如,不返回document.getElementById('searchResult');您可以这样写:
return document.getElementById('searchResult') ? 'true' : '';https://stackoverflow.com/questions/40270764
复制相似问题