Using javascript setInvertal increases processor memory usage

I am developing a .NET .NET application where I need to execute ajax requests using the javascript setInterval () function to update information about some pages.

With each ajax request, I get an xml response of about 68 KB, which I manage to make visual changes to html through jQuery. I set the interval to 2000 milliseconds, but I would like, more precisely, I need to reduce it to 1000 ms.

Unfortunately, with each request, the processor memory increases, and this provokes a browser lock, and the user cannot use it if he does not reload the page. I tested this in Firefox, Internet Explorer and Chrome, but the result is always the same. If I do not do setInvertal (), the problem disappears.

In addition, I tested the scope of all my javascript variables, but I did not find anything wrong, and I believe that Javascript has an efficient garbage collector to clean them.

Hope I explained this issue. Does anyone have an idea to solve it?

Changed: I am using jQuery framework. My code is:

var tim;
$(document).ready(function () {
  tim = window.setInterval("refresh()", 2000);
});

function refresh() {
   $.post("procedures.aspx", $("#formID").serializeArray(), function (data) {
     if (data != ""){
       var xml = $.parseXML(data);

       ... (read and process xml to do changes in the html)

     }
   }
}
+5
source share
2 answers

You did not post a piece of code for verification, so my answer is based on an error that usually occurs when creating such "polling" functions. I also assume that you do everything manually, without using frameworks like jQuery or MooTools.

, , XMLHttpRequest , , setInterval .

, :

var pollInterval = setInterval(function() {
    var xhr = new XMLHttpRequest(); //ouch, this hurts!
    xhr.open('GET', 'url', true);
    xhr.send();
    //etc.
}, 2000);

, XMLHttpRequestObject , , , ( ).

var xhr = new XMLHttpRequest(); //now we can re-use this!
var pollInterval = setInterval(function() {
    xhr.open('GET', 'url', true);
    xhr.send();
    //etc.
}, 2000);


, - - , . , , , ajax . . , .

. XMLHttpRequest , ( ). , , , , .
 


, dom ? , ? ? , - clearInterval, .

. , , - , , API- , (ok, pseudo-realtime at 1s). , API , , , , , dom, , -.

, , , . Dom !

  • : window.setInterval("refresh()", 2000). window.setInterval(refresh, 2000) : javascript setInterval

  • , $.post XHR , . , ? setInterval? , , 10 , XHR "THE" .

  • 500 XML? ? . XML JSON ? .

HTML 11000 . . :

  • ? (speficity xpath). , . , $('p *'), .

  • ?

  • : ( )

  • : ?

  • / : : ? . .. , .

, .

+5

, . , JSON. 4 .

, html- DOM.

webapp , .

+1

All Articles