0

The question is that there are some functions that I call to initial some elements on the page. But after ajax success, I have to re-call those functions again and again in multiple places. I was wondering beside using the following combo

 $(document).ready(function(){
   function A
 }); 

 $(document).ajaxComplete(function(){
   function A
 });

I read that there are something I can do with the setTimeout and clock up the thread to delay the function call from the link http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using.html but I have a hard time digesting it. If someone can break it down for me.

Update:

I meant that when I do an html updated inside multiple ajax success, I have to call function A to re initialize and the code above is my idea but I think there should be a better way

Example

$(document).on('click', 'a', function() {
  $.ajax({
    type: 'GET',
    url: 'some url',
    data: data, 
    success: function(data) {
      $('#some-sector').html(data);
      function A;   <------- to init
    }
  });
});

$(document).on('click', 'b', function() {
  $.ajax({
    type: 'GET',
    url: 'another url',
    data: data, 
    success: function(data) {
      $('#some-sector').html(data);
      function A;   <------- to init
    }
  });
});

Another Update:

So Basically there are some elements on the page that I need to update dynamically by calling function A. And from the example, I have multiple ajax that updates a page. Instead of calling function A in multiple the ajax success, I was wondering if there is a better way to do this. The only thing that i could think of it's the top code.

AirWick219
  • 894
  • 8
  • 27
  • I have read your question 3 times and I still don't know what you want. Can you explain what the problem is? – AME Oct 10 '14 at 07:48
  • See the latest update ... see if that make sense – AirWick219 Oct 10 '14 at 13:07
  • Alright, checkout that [question](http://stackoverflow.com/questions/1960919/jquery-watch-for-domelement-changes), You basically want some kind of a watcher if the element changes? – AME Oct 10 '14 at 16:43

1 Answers1

0

As far as i understand you are calling some group of functions on DOM ready function as

$(document).ready(function(){
function_01();
function_02();
function_03();
});

and want to call the same function on the ajax request

$(document).ajaxComplete(function(){
function_01();
function_02();
function_03();
});

You can define a common function that call internally all the functions that are in need

function callAll()
{
function_01();
function_02();
function_03();
}

and then call as the following

$(document).ready(callAll);

$(document).ajaxComplete(callAll);

at what logic you want to cal this callAll method again and again..

update

try calling the function as below

 $(document).ready(function () {
        function callall()
        {
            alert("call all");
        }
        $(document).ajaxComplete(callall);
  });

and this will call the initialize function after the ajax request is processed

Hope it helps...........

Mayank
  • 1,351
  • 5
  • 23
  • 42