简体   繁体   中英

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 . 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!!!

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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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