Javascript 钛设备加速器中的setTimeout内存泄漏

Javascript 钛设备加速器中的setTimeout内存泄漏,javascript,titanium-mobile,Javascript,Titanium Mobile,我做了一些研究,发现使用setTimeout()会导致严重的内存泄漏,如上所示。我希望能找到一个补救办法或替代办法 当我的许多按钮被触碰时,屏幕上会出现一个小视图。同时,我设置了一个超时,以在3秒钟后淡出小视图。当第一次按下按钮时,我也会清除超时,这样我就不会继续设置多个超时。虽然现在在分析我的代码时,我看到我正在设置一个间隔并清除一个超时。不确定这是否是我的问题的一部分。看起来是这样的: var Modal = Titanium.UI.createView({ width:151,

我做了一些研究,发现使用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()函数放在removeModel()函数中,并从setTimeout中删除了匿名函数。我还纠正了clearTimeout的混乱。我仍然要看看这在扩展的游戏中有多好,但从第一印象来看,这已经解决了我的问题。我希望这能帮助任何有类似问题的人。

我可能通过以下方法解决了这个问题:

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()函数放在removeModel()函数中,并从setTimeout中删除了匿名函数。我还纠正了clearTimeout的混乱。我仍然要看看这在扩展的游戏中有多好,但从第一印象来看,这已经解决了我的问题。我希望这能帮助任何有类似问题的人。

我不知道这是否有帮助,但我注意到,如果我使用:

modalTimer = setTimeout(removeModal(),3000);
但如果我使用

modalTimer = setTimeout(removeModal, 3000);
其中removeModal定义为

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

我不知道这是否有帮助,但我注意到如果我使用:

modalTimer = setTimeout(removeModal(),3000);
但如果我使用

modalTimer = setTimeout(removeModal, 3000);
其中removeModal定义为

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

与您在问题中提到的相反,您正在调用
setTimeout()
clearInterval()
。与您在问题中提到的相反,您正在调用
setTimeout()
clearInterval()
-1:
setTimeout
将函数作为参数,而不是函数调用的结果
removeModal()
预计无法工作。-1:
setTimeout
将函数作为参数,而不是函数调用的结果
removeModal()
预计无法工作。