简体   繁体   English

在Dojo类中递归执行setTimeout调用时是否存在内存泄漏?

[英]Is there memory leak when doing recursively setTimeout invoke within a Dojo class?

We have created an application using Dojo with an clock on the UI. 我们已经使用Dojo在UI上创建了一个应用程序。 But sometimes the application UI just hung-up there and the clock just stopped. 但是有时应用程序UI只是挂在那里,时钟就停止了。 Guessing the JS engine just stopped because the clock is driven by javascript code. 猜测JS引擎刚刚停止,因为时钟由javascript代码驱动。

Not sure following code causes memory leak and then causes the hang-up issue. 不确定以下代码会导致内存泄漏,然后导致挂断问题。 We are using recursively setTimeout invoke to implement the clock. 我们使用递归setTimeout调用来实现时钟。

dojo.declare("xxx.xxx.HomepageHeader", [dijit._Widget, dijit._Templated],
{
widgetsInTemplate: true,
_time :'',
dateUtil: null,

// ....
// ....

prefix :function (value, p)
{
    return (value < 10) ? p + value : value;
},

updateTime :function ()
{
    var d = new Date();
    var _this = this;
    var t = [_this.prefix(d.getHours(), '0'), _this.prefix(d.getMinutes(), '0'), _this.prefix(d.getSeconds(), '0')].join(':');
    _this._time.innerHTML = t;
    _this.dateInfo.innerHTML = this.dateUtil.format(d, "yyyy/MM/dd") + " &nbsp;|&nbsp " + this.dateUtil.format(d, "EEE");
    window.setTimeout( function(){_this.updateTime();}, 100);
}

// ....
// ....
}

Noticed that within the class, the method updateTime used window.setTimeout to recursively invoke itself to update time text on the UI. 注意,在类中,方法updateTime使用window.setTimeout递归调用自身以更新UI上的时间文本。

Is there any memory leak issue here? 这里有内存泄漏问题吗? If the answer is no, is there any possible issue caused the hang up issue? 如果答案是否定的,是否有任何可能导致挂断问题的问题?

Thanks! 谢谢!

This isn't really recursion because setTimeout() schedules the next call to updateTime() for some time in the future and then the current updateTime() actually completes. 这不是真正的递归,因为setTimeout()将下次对updateTime()调用安排在将来的某个时间,然后当前的updateTime()实际上完成了。 So, there is no build-up of the stack frame and thus it's not really recursion. 因此,没有堆栈框架的积累,因此它并不是真正的递归。 Also, I don't see any reason for a memory leak here. 另外,我在这里看不到任何导致内存泄漏的原因。

The scheme you have should be OK and it used often, but you may want to do it less often than every 100ms just to leave more CPU cycles for other things going on the browser. 您拥有的方案应该可以并且经常使用,但是您可能希望比每100毫秒减少一次操作,只是为浏览器上进行的其他操作留出更多的CPU周期。

If you see the clock stop, it is because the JS engine is stuck or looping, but it's probably stuck in code somewhere else other than the actual clock code. 如果您看到时钟停止,那是因为JS引擎卡住或循环了,但是它可能卡在了实际时钟代码以外的其他代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM