问题
I am using jQuery 1.4.4 and use the .get API to check to see if I need to refresh data on a page or not. The problem is that there is a memory leak somewhere in the snippet below that I cannot seem to find. You will notice that I do a lot of potentially useless things here like deleting and add back the timer all in an effort to figure out what is going on. I assumed that doing the .load was causing the problem, but at this point, the .load doesn't occur very frequently at all, only when the data from .get returns "true", which is very fairly infrequent and doesn't coincide with the memory leaking I see using windows task manager. One thing to note is that this does not leak at all using Firefox, seems to be IE specific. I am using IE 8 but don't think that matters much.
<script>
$(document).ready(function() {
    function CheckAlerts() {
    //Must tell Ajax not to cache results
    $.ajaxSetup({ cache: false });
    $.get("AjaxDataCheck/CheckForDataRefresh.cfm", { datacheck: "MyAlerts"},
      function(data) {
       if (data.indexOf("true") >= 0) {
         $.ajaxSetup({ cache: false });
         $('#responsecontainer').load('alertchecking_Inner.cfm');
         //Clearing the timer and recreating may force gargage collection.
         clearTimeout(CheckAlertsTimer);
         delete CheckAlertsTimer;
         CheckAlertsTimer = setTimeout(CheckAlerts,2000);
       }
       else
       {
         //Clearing the timer and recreating may force gargage collection.
         clearTimeout(CheckAlertsTimer);
         delete CheckAlertsTimer;
         CheckAlertsTimer = setTimeout(CheckAlerts,2000);
       }
      });
      CheckAlertsTimer = setTimeout(CheckAlerts,2000);
   };
   CheckAlerts()
});
</script>
Thanks!
回答1:
This occurs as the load() function has an issue releasing dom elements. This is specific to IE, and a workaround is
document.getElementById("responsecontainer").innerHTML = $('#responsecontainer').load('alertchecking_Inner.cfm');
as reported at http://forum.jquery.com/topic/memory-leaks-with-ajax-calls.
来源:https://stackoverflow.com/questions/4763160/memory-leak-when-using-jquery-get-api-and-settimeout