IE8内存泄漏?

发布于 2024-12-06 07:54:24 字数 955 浏览 0 评论 0原文

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

抱歉代码混乱 - 我修改了脚本不是由我选择的,最初来自 Dynamic Drive - 它是由我正在帮助的人使用的,而不是从头开始重写整个事情,我同意功能蔓延。预期的更改是在 vars 之后添加重复和延迟

现在我只想了解哪里可能存在问题 - 我将代码从每秒使用日期对象更改为仅在初始化时使用它。

代码

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

给出了一条

Message: 'thisobj' is null or not an object

在 XP 上的 IE8 中运行大约 9 个小时后,

我现在周末在另一个盒子上运行它,但想知道是否有人可以启发我想知道 IE 可能有什么问题。

HMM - 现在粘贴该函数,我看到 settimeout 位于原型内部 - 突然看起来不正确。

也请随时向我指出一个更好的计数器,可以完成这个计数器的工作例如,在延迟后开始,在给定时间后重复,并在由 CSS 设计的页面上有多个计数器。

更新

尝试 setInterval 使整个倒计时非常不稳定 http://jsfiddle.net/mplungjan/z2AQF/

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

Sorry for the messy code - I have modified a script not chosen by me and originally from Dynamic Drive - it is used by someone I am helping and rather that rewriting the whole thing from scratch, I agreed to the feature creep. The intended changes was to add a repeat after and a delay by vars

Now I just want to understand where there could be a problem - I changed the code from using a date object every second to use it only when initialising.

The code

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

gives a

Message: 'thisobj' is null or not an object

after about 9 hours in IE8 on XP

I am running it myself now on another box over the weekend, but was wondering if someone could enlighten me as to what could be the issue with IE.

HMM - pasting the function right now, I see the settimeout is inside a prototype - that suddenly does not look right.

Please also do feel free to point me to a better counter that can do what this one is doing, e.g. start after a delay, repeat after a given time and have more than one counter on the page styled by CSS.

UPDATE

Trying the setInterval makes the whole coundown very choppy
http://jsfiddle.net/mplungjan/z2AQF/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-12-13 07:54:24

如果确实是内存泄漏,请尝试从传递给 setTimeout 的函数中清除 thisobj 变量:

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

如果你仔细观察,这个函数基本上是一个区间,所以下面的代码更加优化,因为它不会叠加在旧函数上:

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

If it 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 is basically an interval, so the following would be more optimized since it does not stack up on old functions:

cdtime.prototype.updateTime = function () {
    var thisobj = this;
    setInterval(function () {
        thisobj.currentTime += 1000;
    }, 1000);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文