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

发布于 2024-11-15 15:25:23 字数 892 浏览 2 评论 0原文

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

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

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

在我的扩展的主驱动程序中这个函数:

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);
 }
}

使用 consoleService 我发现“checked”的值确实是正确的。我相信问题在于我如何调用clearInterval,但我不太确定如何解决它。

非常感谢任何帮助!

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.

So far I have this bit of code in the .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.

Any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮年慕年 2024-11-22 15:25:23

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

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);
 }
}

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);
 }
}
甩你一脸翔 2024-11-22 15:25:23

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

clearInterval(intervalID);

console.log('reset timer');

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

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);
亣腦蒛氧 2024-11-22 15:25:23

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

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

 else {
        window.clearInterval(interval);
 }

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

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

 else {
        window.clearInterval(interval);
 }
稚气少女 2024-11-22 15:25:23

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

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

以下是 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

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

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

Below are some examples of the JavaScript variable scope. var is used to define a variable in the current scope. Leaving var always creates or changes a local variable.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文