2

I have multiple ajax requests with javascript code as response, and I need to stop execution of previos javascript code once new code was loaded, I mean to stop intervals, functions, timeouts...

My Code :

$.get("/ucmd", params, function(data) {
  var $data = document.createElement("div");
  $data.innerHTML = data;
  var em = $data.getElementsByTagName("*");
  for(var i=0;i<em.length;i++)
  {
    if(em[i].tagName === "SCRIPT")
    {
     if($("#"+em[i].id)) document.body.removeChild($("#"+em[i].id));
     var script = document.createElement("script");
     script.id = em[i].id;
     script.language="javascript";
     script.type="text/javascript";
     script.innerHTML = em[i].innerHTML;
     document.body.appendChild(script);
    }
  }
});

Is it possible to make it to stop execution of previous code when new code is loaded?

P.S: I would like to use pure javascript code.

My Solution :

  window.$timeout = {};
  window.$interval = {};

  window.$setTimeout = window.setTimeout;
  window.$setInterval = window.setInterval;
  window.$clearTimeout = window.clearTimeout;
  window.$clearInterval = window.clearInterval;

  window.setTimeout = function(id, code, delay) {
    if(typeof $timeout[id] !== "undefined") clearTimeout(id);
    $timeout[id] = $setTimeout(code, delay);
  }; 

  window.clearTimeout = function(id) {
    $clearInterval($timeout[id]);
    delete $timeout[id];
  };

  window.setInterval = function(id, code, delay) {
    if(typeof $interval[id] !== "undefined") clearInterval(id);
    $interval[id] = $setInterval(code, delay);
  }; 

  window.clearTimeout = function(id) {
    $clearInterval($interval[id]);
    delete $interval[id];
  };
John
  • 7,500
  • 16
  • 62
  • 95

3 Answers3

2

To stop an existing call using timeout(), you can use:

clearTimeout(t);

You may be able to keep a global var to keep cou8nt of times you function is called and code the clearTimeout() around that...

make your calls like:

t = setTimeout("thing()",1000);
Brian
  • 8,418
  • 2
  • 25
  • 32
2

If you don't keep track of setInterval() and setTimeout() identifiers, this is simply not possible unless you refresh the whole page.

How to stop all timeouts and intervals using javascript?


To clear all event handlers is also not trivial; you'd have to traverse the whole tree and call .unbind() on all of them. Perhaps you could limit the scope?

How to remove all Click event handlers in Jquery


The only easy thing is making sure no functions are running, because by the time your AJAX response is being handled you're pretty sure no other code is running :)

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

You can clear timeouts and intervals with these functions clearInterval(id) & clearTimeout(id) (Google them)

As of functions you can do this:

// Orginal function
window.myFunc = function(a,b,...){ ...your code here... }


// New function after ajax load
$.ajax({ 
    url : '/blablabla.php',
    type : 'GET',
    data : params,
    succes : function(){
        ......
        window.myFunc = function(a,b,...){ ...your NEW code here... }
    }
});
Ties
  • 5,726
  • 3
  • 28
  • 37