Akhil F
Akhil F

Reputation: 7740

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.

Upvotes: 3

Views: 1181

Answers (1)

Danack
Danack

Reputation: 25701

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

Upvotes: 2

Related Questions