Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing jQuery and Memory Leaks
    text
    copied!<p>I have been using jQuery for over a couple of months and read up on Javascript memory leaks for a few days. I have two questions regarding memory leaks and jQuery:</p> <ol> <li><p>When I bind (using .bind(...)) do I have to unbind them (.unbind()) if I leave the page/refresh to avoid memory leaks or does jQuery remove them for me?</p></li> <li><p>Concerning closures, I read that they can lead to memory leaks if used incorrectly. If I do something such as:</p> <p>function doStuff( objects ){ //objects is a jQuery object that holds an array of DOM objects var textColor = "red"; objects.each(function(){ $(this).css("color", textColor ); }); }</p> <p>doStuff( $( "*" ) );</p></li> </ol> <p>I know that the above code is stupid (better/simpler r ways of doing this) but I want to know if this causes circular references/closure problems with .each and if it would cause a memory leak. If it does cause a memory leak, how would I rewrite it (usually similar method) to avoid a memory leak?</p> <p>Thanks in advance.</p> <p>Edit: I have another case similar to question 2 (which I guess makes this part 3).</p> <ol> <li><p>If have something like this:</p> <p>function doStuff( objects ){ //iframe objects var textColor = "red";</p> <pre><code>function innerFunction() { $(this).contents().find('a').css("color", textColor ); } objects.each(function(){ //I can tell if all 3 are running then we //have 3 of the same events on each object, //this is just so see which method works/preferred //Case 1 $(this).load(innerFunction); //Case 2 $(this).load(function(){ $(this).contents().find('a').css("color", textColor ); }); //Case 3 $(this).load(function(){ innerFunction(); }); }); </code></pre> <p>}<br> doStuff( $( "iframe" ) );</p></li> </ol> <p>There are 3 cases above and I would like to know which method (or all) would produce a memory leak. Also I would like to know which is the preferred method (usually I use case 2) or better practice (or if these are not good what would be better?). </p> <p>Thanks again!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload