huzefa biyawarwala
huzefa biyawarwala

Reputation: 657

setTimeout isn't reached by page.open in PhantomJS

I am trying to call nextpage function from the page.open in my script, but when I run this I get no output in console and no errors. I have debugged about it since a long time now, but haven't got any solution. The code is below:

var page = require('webpage').create();
var url = 'https://www.youtube.com/user/jannunzi/about';

page.open(url, function (status) {

    setTimeout(function(){
        nextpage();
        console.log("inside timeout.....");
    },5000);

    phantom.exit();
});

function nextpage(){
    console.log("huzefa...........................");
}

Here, none of the both console messages gets printed in the console.

Upvotes: 1

Views: 385

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

How could it when you immediately exit PhantomJS (phantom.exit();)? JavaScript's setTimeout() is asynchronous, so statements that come afterwards are evaluated before the callback is called. You should move the exit into the callback for setTimeout().

Upvotes: 1

Related Questions