setTimeout内存泄漏钛Appcelerator

setTimeout memory leak in Titanium Appcelerator

本文关键字:Appcelerator 泄漏 内存 setTimeout      更新时间:2023-09-26

我做了一点研究,发现使用setTimeout()会导致糟糕的内存泄漏,正如这篇文章所见。我希望能找到一种治疗方法或替代方法。

我所拥有的是当我的许多按钮中的任何一个被触摸时出现在屏幕上的小视图。同时,我设置了一个超时,在3秒后逐渐淡出小视图。当第一次按下按钮时,我还清除了超时,这样我就不会继续设置多个超时。虽然现在在分析我的代码时,我看到我设置了一个间隔并清除了超时。我不确定这是不是我的问题。它看起来像这样:

var Modal = Titanium.UI.createView({
    width:151,
    height:83,
    owner: null,
    myView: null,
});
var modalTimer;
var addModal = function(){
    clearInterval(modalTimer);
    theView.add(Modal);
    modalTimer = setTimeout( function() {
        removeModal();
        changeTurn();
},3000);
}
playerButton.addEventListener('click',function(){
    addModal(); 
});

谢谢! !

我可能已经用以下方法解决了这个问题:

var Modal = Titanium.UI.createView({
    width:151,
    height:83,
    owner: null,
    myView: null,
});
var modalTimer;
var addModal = function(){
    clearTimeout(modalTimer);
    theView.add(Modal);
    modalTimer = setTimeout(removeModal(),3000);
}
playerButton.addEventListener('click',function(){
    addModal(); 
});

我把我的changeTurn()函数放在我的removeModal()函数中,并从setTimeout中删除了匿名函数。我还纠正了clearTimeout混淆。我还需要看看这在扩展游戏玩法时的表现如何,但从第一印象来看,这已经解决了我的问题。我希望这对有类似问题的人有所帮助。

我不知道这是否有帮助,但我注意到我的应用程序崩溃,如果我使用:

modalTimer = setTimeout(removeModal(),3000);

但是如果我使用

modalTimer = setTimeout(removeModal, 3000);

removeModal定义为

var removeModal = function()
{...};