简体   繁体   English

Titanium Appcelerator中的setTimeout内存泄漏

[英]setTimeout memory leak in Titanium Appcelerator

I've done a little bit of research and have found that using setTimeout() causes a bad memory leak, as seen on this post . 我已经做了一点点研究,并已发现,使用的setTimeout()会导致不良内存泄漏,就因为看到这个帖子 I'm hoping to find a remedy or an alternative. 我希望找到一种补救方法或替代方法。

What I have is a small view appearing on the screen when any of my many buttons is touched. 当我触摸许多按钮中的任何一个时,屏幕上都会出现一个小视图。 At the same time I set a timeout to fadeout the small view after 3 seconds. 同时,我设置了一个超时以在3秒后淡出小视图。 When the button is first pressed, I also clear the timeout so that I don't continue setting multiple ones. 首次按下该按钮时,我还清除了超时,以便不再继续设置多个按钮。 Though now while analyzing my code I see that I'm setting an interval and clearing a timeout. 尽管现在在分析代码时,我看到我正在设置时间间隔并清除超时。 Not sure if this is part of my issue. 不知道这是否是我的问题。 It looks like this: 看起来像这样:

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(); 
});

Thanks!!! 谢谢!!!

I may have solved this issue with the following: 我可能已通过以下方法解决了此问题:

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(); 
});

I put my changeTurn() function in my removeModal() function, and removed the anonymous function from setTimeout. 我将changeTurn()函数放入removeModal()函数中,并从setTimeout中删除了匿名函数。 I also corrected the clearTimeout confusion. 我还纠正了clearTimeout的困惑。 I still have to see how well this holds up over extended gameplay, but from first impressions this has fixed my issues. 我仍然必须看到它在扩展游戏性方面的表现如何,但是从第一印象开始,这已经解决了我的问题。 I hope this helps anyone with similar issues. 希望这对遇到类似问题的人有所帮助。

I don't know if this helps but I have noticed that my app crashes if i use: 我不知道这是否有帮助,但是我注意到,如果我使用以下应用程序会崩溃:

modalTimer = setTimeout(removeModal(),3000);

but doesn't if i use 但是如果我用不

modalTimer = setTimeout(removeModal, 3000);

where removeModal is defined as 其中removeModal定义为

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

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

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