Jquery auto refresh div

JQuery auto-update uses most of the browser memory. Is there any way to stop this. I had a 2x update every 3 seconds, but I moved it to 9 and 15 seconds. This helped a little longer to keep the window open on my site, the more memory required, until finally the browser worked.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script> <script> var auto_refresh = setInterval( function () { $('#details2').load('links2.php').fadeIn("slow"); }, 15000); // refresh every 10000 milliseconds</script> 
+7
jquery memory-leaks refresh
source share
3 answers

You can try to skip loading () and use $ .ajax instead. I know load (); this is an ajax request, but I seem to recall that it retrieves the entire script. Try querying the script, do the calculations in the database, and return the data as json. I assume that you are sending the full html with the data from the database request. Try this instead of json.

You will receive data as objects, for example, such as.

 {"variable":"foo"} 

Then you can get the data with a simple expression.

 $.ajax({ url: "links2.php", type: "POST", dataType: "json", success: function(data){ // data here is returned as objects since it json $.each(data, function(key, value) { $("#details2").empty().append(value.variable); }); } }); 

I think this should not be your memory leak and, ultimately, your browser crash, although you call it every second or so. Try it and let me know how this happens.

Good luck

+2
source share

Try changing it to this:

 // ... $('#details2').empty().load('links2.php').fadeIn('slow'); 

This can lead to jQuery explicit instructions to clear the container first so that it can release any event handlers, etc. (although it is not clear that there will be any handlers ...)

edit - it doesn't really matter; I checked the jQuery sources, and it looks like the .html() call (which load() does, I'm sure) always invokes empty() anyway.

0
source share

Although the answer has been approved, but I must tell you this. I had the same problem.

I found the problem in the src of the jQuery file. I used the jQuery site url as the source and it increased my computer usage to 99%. But then I downloaded the whole JQuery Script and saved it in my website directory, I used it in my source, and then there was no problem using the computer or memory. Try it too ..

0
source share

All Articles