Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function called once, but firing multiple times

The following is a snippet from a PhantomJS script. It tracks dynamic content on an AJAXd webpage. 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);
                    };
                };
            });
        };
    };

Logs 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 being called 3 times.

like image 382
Aakil Fernandes Avatar asked Jan 31 '26 16:01

Aakil Fernandes


1 Answers

Apparently PhantomJS calls page.open multiple times if there are redirects or iFrames being loaded on the page.

There are some suggestions of how to handle that on the PhantomJS bug tracker.

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

like image 133
Danack Avatar answered Feb 03 '26 06:02

Danack