views:

596

answers:

1

Ok. here's the scenario:

function DataFeed(){

  function PopulateData()
  {
    $('div#example').load('http://www.example.com', fxnCallBack);
  };

  function fxnCallBack()
  {
    PopulateData();
  }

  this.activator = function() {
    PopulateData();
  }

};

var example_obj = new DataFeed;
example_obj.activator();

In the above code, the ajax .load gets executed once, then callback executes. But the callback doesn't start the ajax function again?

Thanks in advance.

edit- why doesn't it display new line properly -.-

+1  A: 
InternalError: too much recursion

JavaScript engines normally have a max limit in the number of recursions or the time recursive execution may take. Use setInterval instead:

function DataFeed() {
    var interval;
    function PopulateData() { 
        $('div#example').load('http://www.example.com', function(data) {
            if(data == "clear_interval")
                interval = clearInterval(interval); // clear the interval
        }); 
    }

    this.activator = function() { 
        interval = setInterval(PopulateData, 1000); // run every second
    };
}
var example_obj = new DataFeed();
example_obj.activator();
Luca Matteis
Well, that's not exactly what I was intending.I'm looking to implement some logic in the callback function to decide if more data need to be retrieved.eg. if ($('div#example tr').length<5) PopulateData();
Thanks for the idea though. I'll consider doing it this way, if it doesn't work out the way I want it to.
@Jace: I changed my answer to show you how you can implement the clearnInterval to stop the recursion.
Luca Matteis