I have been using jQuery for several months and have been reading about Javascript memory leaks for several days. I have two questions regarding memory leaks and jQuery:
When I bind (using .bind (...)), do I need to untie them (.unbind ()) if I leave the page / update to avoid memory leaks or does jQuery delete them for me?
Regarding closures, I read that they can lead to a memory leak if used improperly. If I do something like:
function doStuff (objects) {// objects - jQuery object containing an array of DOM objects var textColor = "red"; objects.each (function () {$ (this) .css ("color", textColor);}); }
doStuff ($ ("*"));
I know that the code above is dumb (better / easier) for this, but I want to know if this causes circular linking / closure problems with .each, and if this leads to a memory leak. If this causes a memory leak, how can I rewrite it (usually a similar method) to avoid a memory leak?
Thanks in advance.
Edit: I have another case similar to question 2 (which I think makes this part 3).
If there is something like this:
function doStuff (objects) {// iframe objects var textColor = "red";
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(); }); });
}doStuff ($ ("iframe"));
There are three cases above, and I would like to know which method (or all) will lead to a memory leak. Also I would like to know which one is preferred (usually I use case 2) or better (or if it is not good, what would be better?).
Thanks again!
1) . .
2) , jQuery .each() , , , , (.. ) . , , objects.
.each()
objects
, .each() - , $('div:eq(0)').bind() ( , objects, , ), , , .bind(), objects, , , , .
$('div:eq(0)').bind()
.bind()
objects = null; .
objects = null;
, JS, . , , , , , , .
javascript, , .
, . , jQuery 1.5 IE8
, dom node, , , , . DOM node .
1., , .unbind() . .
.unbind()
1, , Internet Explorer (- , );) Google v2, , document.onunload (GUnload).
document.onunload
2, . , this textColor .
this
textColor
, . , .
,