以下是 PhantomJS 脚本的片段。它跟踪 AJAXd 网页上的动态内容。 track() 被调用了一次,但由于某些原因 page.open() 被调用了 3 次
function track(url){
console.log('Tracking',url);
var page = require('webpage').create();
console.log('check2')
if(page){
console.log('check4');
page.open(url, function (status) {
console.log('check3');
if (status !== 'success') {
console.log('Unable to load the address!');
setTimeout(function(){start();},1000);
setTimeout(function(){page.release();},5000);
}
else {
console.log('check');
var i = 0;
var last_winner = false;
var logged_once = false;
var interval = false;
if(!interval){
interval = setInterval(function(){
var scraping = scrape(page);
var date = new Date();
var time = date.getTime();
if(scraping){/*Bunch of console logs*/}
else{
console.log('Bidding ended');
clearInterval(interval);
setTimeout(function(){start();},1000);
setTimeout(function(){page.release();},5000);
}
scraping = false;
},1000);
};
};
});
};
};
将以下内容记录到控制台:
Tracking http://www.google.com
check2
check4
check3
check
check3
check
check3
check
出于某种原因我无法弄清楚,page.open() 被调用了 3 次。
最佳答案
显然,如果页面上加载了重定向或 iFrame,PhantomJS 会多次调用 page.open。
在 PhantomJS 错误跟踪器上有一些关于如何处理该问题的建议。
http://code.google.com/p/phantomjs/issues/detail?id=353&q=open%20callback
关于javascript - 函数调用一次,但触发多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10115779/