Javascript:setInterval是';使对象无效时不会删除

Javascript:setInterval是';使对象无效时不会删除,javascript,object,javascript-objects,Javascript,Object,Javascript Objects,即使对象为null,setInterval函数仍保持运行,我应该先将setInterval变量更改为null,还是执行其他操作?此外,即使setInterval仍在运行,GC是否会删除该对象 Test = function(){ function start(){ // setTimout for controllable FPS var loop = setInterval(function(){ console.log("TES

即使对象为null,setInterval函数仍保持运行,我应该先将setInterval变量更改为null,还是执行其他操作?此外,即使setInterval仍在运行,GC是否会删除该对象

Test = function(){
    function start(){
        // setTimout for controllable FPS
        var loop = setInterval(function(){
            console.log("TEST");
        }, 1000);
    }

    start();
};


var test = new Test();

setTimeout(function(){
    console.log("DIE!!");
    test = null;
}, 2000);

setInterval返回的值只是一个用于标识间隔引用的数字。您不能将其设为空-您需要调用引用上的window.clearInterval

在你发布的代码中还有其他一些东西是没有意义的。例如,在函数中声明一个函数,然后只调用一次。我认为这可能更接近你想要的:

var Test = function(){
  this.start();
}
Test.prototype = {
  loop : null,
  start : function(){
    this.loop = window.setInterval(function(){
      console.log('TEST');
    }, 1000);
  },
  stop : function(){
    window.clearInterval(this.loop);
  }
}

var test = new Test();
window.setTimeout(function(){
  test.stop();
}, 5000);
这将运行间隔5次

FWIW,这里并没有涉及GC。只要存在对任何变量的引用,它就不会被收集


HTH

setInterval返回的值只是一个用于标识间隔引用的数字。您不能将其设为空-您需要调用引用上的window.clearInterval

在你发布的代码中还有其他一些东西是没有意义的。例如,在函数中声明一个函数,然后只调用一次。我认为这可能更接近你想要的:

var Test = function(){
  this.start();
}
Test.prototype = {
  loop : null,
  start : function(){
    this.loop = window.setInterval(function(){
      console.log('TEST');
    }, 1000);
  },
  stop : function(){
    window.clearInterval(this.loop);
  }
}

var test = new Test();
window.setTimeout(function(){
  test.stop();
}, 5000);
这将运行间隔5次

FWIW,这里并没有涉及GC。只要存在对任何变量的引用,它就不会被收集


HTH

setInterval返回的值只是一个用于标识间隔引用的数字。您不能将其设为空-您需要调用引用上的window.clearInterval

在你发布的代码中还有其他一些东西是没有意义的。例如,在函数中声明一个函数,然后只调用一次。我认为这可能更接近你想要的:

var Test = function(){
  this.start();
}
Test.prototype = {
  loop : null,
  start : function(){
    this.loop = window.setInterval(function(){
      console.log('TEST');
    }, 1000);
  },
  stop : function(){
    window.clearInterval(this.loop);
  }
}

var test = new Test();
window.setTimeout(function(){
  test.stop();
}, 5000);
这将运行间隔5次

FWIW,这里并没有涉及GC。只要存在对任何变量的引用,它就不会被收集


HTH

setInterval返回的值只是一个用于标识间隔引用的数字。您不能将其设为空-您需要调用引用上的window.clearInterval

在你发布的代码中还有其他一些东西是没有意义的。例如,在函数中声明一个函数,然后只调用一次。我认为这可能更接近你想要的:

var Test = function(){
  this.start();
}
Test.prototype = {
  loop : null,
  start : function(){
    this.loop = window.setInterval(function(){
      console.log('TEST');
    }, 1000);
  },
  stop : function(){
    window.clearInterval(this.loop);
  }
}

var test = new Test();
window.setTimeout(function(){
  test.stop();
}, 5000);
这将运行间隔5次

FWIW,这里并没有涉及GC。只要存在对任何变量的引用,它就不会被收集


HTH

为什么将
test
设置为
null
会对
setInterval
产生任何影响?使用
窗口。清除超时(测试)
为什么将
test
设置为
null
会对
setInterval
产生任何影响?使用
窗口。清除超时(测试)
为什么将
test
设置为
null
会对
setInterval
产生任何影响?使用
窗口。clearTimeout(test)
为什么将
test
设置为
null
会对
setInterval
产生任何影响?使用
窗口。clearTimeout(test)