/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Major JS memory leak

I don’t even know what a memory leak is….but I know I have certainly created one….in IE7 anyway. Safari, Opera, and FF work fine.

The page: [url]http://www.horizonconference.org/samplealbum.php[/url]

Click on a picture to bring up the picture div. I have created a JS function that switches the next and previous images by using your arrow keys. In the source code in [url]http://www.horizonconference.org/js/albums.js[/url] the function that handles the keydown events is below:

[code]
function swapPhoto(e) {
var found = false;
//first determine if the window is open – if it isn’t then remove the event listener ond do nothing
if(xGetElementById(“photoWindowDiv”)) {
//get all children elements and make sure one of them is display: block
theDivs = xGetElementById(“photoWindowDiv”).getElementsByTagName(“div”);
for(i=0;i<theDivs.length;i++) {
if(theDivs[i].parentNode.id == “photoWindowDiv”) {
if(theDivs[i].style.display == “block”) {
found = theDivs[i];
break;
}
}
}
var next;
var prev;

if(found) {
var ev=(!e)?window.event:e;//IE:Moz
//get the next and previous values
nextprev = found.getElementsByTagName(“span”);
for(i=0;i<nextprev.length;i++) {
switch(nextprev[i].className) {
case “next”: next = nextprev[i].innerHTML; break;
case “prev”: prev = nextprev[i].innerHTML; break;
}
}
switch(ev.keyCode) {
case 37: showPhoto(prev); break;
case 39: showPhoto(next); break;
}
}
}
if(!found) {
xRemoveEventListener(document,’keydown’,keypress,false);
}
}
[/code]

Basically what it does is it determines first if an image is being displayed on the screen. Sometimes this onkeydown event might be fired when there isn’t any need for it and if that’s the case I remove it at the bottom by the if(!found) statement.

Then it will find the next and previous photo ids which are stored in the DOM and then call a function using those values depending on what key was pushed.

When you do it in FF, everything works great and you can actually scroll through the photos pretty fast by holding your finger down on one of the arrow keys. In IE7 however, it gets slower and slower and slower every time you push an arrow key. I have no idea how to find the “leak” and I am hoping some experts can help me out. Thanks.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscauthorOct 09.2008 — So I fixed it......don't even know how I did it but it's fixed.
×

Success!

Help @aj_nsc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...