0

I have this beautiful code, all I want to make some pause between visits, so I add a 'setinterval', but this not works:

var page = require('webpage').create();

// the urls to navigate to
var urls = [
    'http://blogger.com/',
    'https://github.com/',
    'http://reddit.com/'
                ];
    var i = 0;

// the recursion function
var genericCallback = setInterval(function () {
    return function (status) {
        console.log("URL: " + urls[i]);
        console.log("Status: " + status);
        // exit if there was a problem with the navigation
        if (!status || status === 'fail') phantom.exit();

        i++;

        if (status === "success") {

            //-- YOUR STUFF HERE ---------------------- 
            // do your stuff here... I'm taking a picture of the page
            page.render('example' + i + '.png');
            //-----------------------------------------

            if (i < urls.length) {

                // navigate to the next url and the callback is this function (recursion)
                page.open(urls[i], genericCallback());

            } else {
                // try navigate to the next url (it is undefined because it is the last element) so the callback is exit
                page.open(urls[i], function () {
                    phantom.exit();
                });
            }
        }
    };
},2000);

// start from the first url
page.open(urls[i], genericCallback());

the screenshot with the error I get: enter image description here maybe someone could help me and heal this code? Because I'm new to JS and to PhantomJS, any help will be apreciate. I got this code from another stackoverflow answer here - Using Multiple page.open in Single Script but I can't comment to the author , because I don't have 50 reputation

Community
  • 1
  • 1
Great Serg
  • 71
  • 1
  • 2
  • 10
  • 1
    `status` is not defined.... `page.open(url, callback(status));` ... the script given by that author is generally incorrect. – Flash Thunder Feb 26 '17 at 13:05
  • still the same error=( Could it be something else? – Great Serg Feb 26 '17 at 13:09
  • But I use his code before and it works. Now I edit it with setinterval - and It's not working. then I do all that you said and get same error. – Great Serg Feb 26 '17 at 13:12
  • 1
    check my answer ... sorry edited, I am writting from phone, but should work now – Flash Thunder Feb 26 '17 at 13:18
  • 1
    just checked and my example works fine... it saves images from all sites from urls array with 2 second interval between requests... your example is wrong in all ways... it creates multiple intervals not timeouts... interval is that it repeats it every 2 seconds, and then callbacks to itself creating new interval... totally wrong. That way, it refers to page many times at once. – Flash Thunder Feb 26 '17 at 13:25
  • Thank you mate, this code working like a charm! – Great Serg Feb 26 '17 at 16:26
  • Possible duplicate of [Using Multiple page.open in Single Script](http://stackoverflow.com/questions/16996732/using-multiple-page-open-in-single-script) – Vaviloff Feb 27 '17 at 05:07

1 Answers1

2

It should rather be something like this:

var page = require('webpage').create();

var urls = ['http://blogger.com/','https://github.com/','http://reddit.com/'];
var i = 0;

function OpenPage(){
    setTimeout(function(){
        page.open(urls[i], function(status) {
            if (status == 'success') {
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },2000);
}

OpenPage();
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
  • Dear, Flash Thunder, can you help me with this question please? http://stackoverflow.com/questions/42475300/phantomjs-doesnt-resize-the-window – Great Serg Feb 26 '17 at 23:00