I am writing a PhantomJS program that should open a page, track its time and write the loading time in the console. My goal is to make this function run for 5 times.
Problem is that when I write "for" loop which executes only 1 time - function runs correctly, but when I try to run it 2 times, I get the error message that it failed to open the address. It seems that address is not passed second time. I have no idea what could go wrong anymore. This is my code:
var page = require('webpage').create(),
system = require('system'),
t;
//Opening a page and tracking page load time
var loadpage = function (){
address = 'http://www.google.com';
t = Date.now();
page.open(address, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading ' + address);
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
};
for (var i = 0; i <2 ; i++) {
loadpage(i);
}