Memory leak in IE8?

http://jsfiddle.net/mplungjan/SyHFR/

Sorry for the messy code - I changed a script that I didn’t select and originally from Dynamic Drive - it is used by someone I help and, rather, rewriting it all from scratch, I agreed to a creep function.The supposed changes were to add repeat after and delay with vars

Now I just want to understand where the problem may be - I changed the code to use the date object every second, so that it could be used only during initialization.

Code

cdtime.prototype.updateTime=function(){ var thisobj=this; this.currentTime+=1000; // one second setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second } 

gives

Message: 'thisobj' is null or not an object

after about 9 hours in IE8 on XP

I run it myself now in a different box over the weekend, but wondered if anyone could enlighten me about what could be a problem with IE.

HMM - inserting a function right now, I see that the setting is inside the prototype - it suddenly looks wrong.

Please also feel free to tell me the best counter that can do what this one does, for example. start after a delay, repeat after a specified time and have more than one counter on the page created by CSS.

UPDATE

Trying setInterval makes all coundown very mutable http://jsfiddle.net/mplungjan/z2AQF/

+4
source share
1 answer

If this is really a memory leak, try clearing the thisobj variable from the function passed to setTimeout :

 cdtime.prototype.updateTime = function () { var thisobj=this; this.currentTime+=1000; // one second setTimeout(function () { thisobj.updateTime(); thisobj = null; }, 1000); //update time every second }; 

If you look closer, this function will be basically an interval, so the following will be more optimized, since it does not add up to the old functions:

 cdtime.prototype.updateTime = function () { var thisobj = this; setInterval(function () { thisobj.currentTime += 1000; }, 1000); }; 
+1
source

All Articles