2011-12-14 29 views
1

我已经做了一点研究,并发现使用setTimeout()导致不良内存泄漏,如this post所示。我希望找到一种补救办法或替代方案。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(); 
}); 

谢谢!

+0

与你在你的问题中提到的你调用`setTimeout()`和`clearInterval()`相反。 – nnnnnn 2011-12-14 05:46:19

+0

非常真实,将它们混合起来...... – gjunkie 2011-12-14 06:41:14

回答

1

我可能已经解决了这个问题,有以下几点:

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的困惑。我仍然必须看到,这在扩展游戏玩法方面有多好,但从第一印象来看,这已经解决了我的问题。我希望这可以帮助任何有类似问题的人。

0

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

modalTimer = setTimeout(removeModal(),3000); 

但不如果我使用

modalTimer = setTimeout(removeModal, 3000); 

其中removeModal被定义为

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