Sunday, April 3, 2011

Ajax call back function scope & chaining Ajax request with callback.

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 -.-

From stackoverflow
  • 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 : @Jace: I changed my answer to show you how you can implement the clearnInterval to stop the recursion.

0 comments:

Post a Comment