Titanium Appcelerator 中的 setTimeout 内存泄漏
我做了一些研究,发现使用 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();
});
谢谢!!!
I've done a little bit of research and have found that using setTimeout() causes a bad memory leak, as seen on this post. 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. 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!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能已经通过以下方法解决了这个问题:
我将changeTurn()函数放入removeModal()函数中,并从setTimeout中删除了匿名函数。我还纠正了clearTimeout 的混乱。我仍然需要看看这在扩展游戏中的表现如何,但从第一印象来看,这已经解决了我的问题。我希望这对有类似问题的人有所帮助。
I may have solved this issue with the following:
I put my changeTurn() function in my removeModal() function, and removed the anonymous function from setTimeout. I also corrected the clearTimeout confusion. 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.
我不知道这是否有帮助,但我注意到如果我使用,我的应用程序会崩溃:
则不会崩溃
但如果我使用removeModal 定义为的位置,
I don't know if this helps but I have noticed that my app crashes if i use:
but doesn't if i use
where removeModal is defined as