javascript clearInterval() 不会停止 setInterval() - Firefox 扩展开发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6345577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:21:12  来源:igfitidea点击:

clearInterval() is not stopping setInterval() - Firefox Extension Development

javascriptcheckboxfirefox-addonxulsetinterval

提问by Kotsu

I am working on a modification of tamper data that will allow me to send the HTTP request/responses it observes to a server. So far, that functionality has been implemented correctly. The next step is to automate this process, and I wish to use a toolbarmenu button of type 'checkbox' to toggle this functionality on and off.

我正在修改篡改数据,这将允许我将它观察到的 HTTP 请求/响应发送到服务器。到目前为止,该功能已正确实现。下一步是自动化此过程,我希望使用“复选框”类型的工具栏菜单按钮来打开和关闭此功能。

So far I have this bit of code in the .XUL:

到目前为止,我在 .XUL 中有这么一段代码:

<toolbarbutton id="tamper.autosend" label="&tamper.toolbar.autosend;" type="checkbox" oncommand="oTamper.toggleTimer();"/>

And this function in the main driver of my extension:

这个功能在我的扩展的主驱动程序中:

toggleTimer : function() {
 var checked = document.getElementById('tamper.autosend').checked;

 var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);

 consoleService.logStringMessage(checked);

 if (checked) {
        var interval = window.setInterval(function(thisObj) { thisObj.sendResults(true); }, 1000, this);
 }

 else {
        window.clearInterval(interval);
 }
}

Using the consoleService I see that the value of 'checked' is indeed correct. I believe the problem lies with how I am calling clearInterval, but I'm not exactly sure how to remedy it.

使用 consoleService 我看到 'checked' 的值确实是正确的。我相信问题在于我如何调用 clearInterval,但我不确定如何解决它。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by Senad Me?kin

You have defined interval inside if try to declare your variable on the start

如果尝试在开始时声明变量,则在内部定义了间隔

var interval = 0;
toggleTimer : function() {
 var checked = document.getElementById('tamper.autosend').checked;

 var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);

 consoleService.logStringMessage(checked);

 if (checked) {
        interval = window.setInterval(function(thisObj) { thisObj.sendResults(true); }, 1000, this);
 }

 else {
        window.clearInterval(interval);
 }
}

回答by richardd

Your doing it wrong, each time you want to set the new interval you should clear it first

你做错了,每次你想设置新的间隔时,你应该先清除它

clearInterval(intervalID);

console.log('reset timer');

intervalID = setInterval(function () {
    console.log('tick');
}, refreshInterval);

回答by reko_t

You're storing the intervalin a local variable; the value is lost after the function returns, next time you attempt to clearIntervalan undefined variable. Store the interval in i.e. a global variable instead:

您将 存储interval在局部变量中;函数返回后,该值将丢失,下次您尝试clearInterval使用未定义的变量时。将间隔存储在 ie 全局变量中:

 if (checked) {
        window.interval = window.setInterval(function(thisObj) { thisObj.sendResults(true); }, 1000, this);
 }

 else {
        window.clearInterval(interval);
 }

回答by Midas

Ofcourse, because intervalis defined as a private variable. It is defined in the toggleTimerfunction and is destroyed when the function ends.

当然,因为interval被定义为私有变量。它在toggleTimer函数中定义,并在函数结束时销毁。

Use interval = window.setInterval()instead of var interval = window.setInterval()to define a global variable that is accessible later for clearInterval.

使用interval = window.setInterval()而不是var interval = window.setInterval()定义稍后可访问的全局变量clearInterval

Below are some examples of the JavaScript variable scope. varis used to define a variable in the currentscope. Leaving varalways creates or changes a local variable.

下面是 JavaScript 变量范围的一些示例。var用于定义当前作用域中的变量。离开var总是创建或改变一个局部变量。

function func1() {
    i = 1; // global
}
func1();
alert(i); // 1

var j = 2;
function func2() {
    var j = 3; // private
}
func2();
alert(j); // 2

k = 4;
function func3() {
    k = 5; // global
}
func3();
alert(k); // 5

var l = 6;
function func4() {
    l = 7; // global
}
func4();
alert(l); // 7

function func5() {
    var m = 6; // private
}
func5();
alert(m); // undefined