Reputation: 379
Is it possible to open page, do my things and close it. And after a timeout visit it once again to see the content changes (page has many js functions). Trying to open the page twice in a row makes PhantomJS behave unpredictable. What is the solution then?
Upvotes: 1
Views: 316
Reputation: 61892
Most the execution in PhantomJS is asynchronous, so you have to open a page only after the first page load was completed:
page.open(url, function(){
setTimeout(function(){
// do something
page.open(url, function(){
setTimeout(function(){
// do something
phantom.exit();
}, 5000); // 5 seconds
});
}, 5000); // 5 seconds
});
or even better using recursion:
var i = 0;
function run(){
if (i > 100) { // stop execution at some point
phantom.exit();
}
page.open(url, function(){
// do what you have to do
setTimeout(run, 5000); // run again in 5 seconds
});
}
run();
Upvotes: 1