The function is called once, but it works several times

Below is a snippet from the PhantomJS script. It tracks dynamic content on the AJAXd web page. track () is called once, but for some reason page.open () is called 3 times

    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);
                    };
                };
            });
        };
    };

Prints the following to the console:

Tracking http://www.google.com
check2
check4
check3
check
check3
check
check3
check

For some reason I can't figure out page.open () is called 3 times.

+3
source share
1 answer

Apparently, PhantomJS calls page.open several times if redirects or iFrames are loaded on the page.

There are some tips on how to handle this file tracker on PhantomJS.

http://code.google.com/p/phantomjs/issues/detail?id=353&q=open%20callback

+2
source

All Articles