clearInterval() 不会停止 setInterval() - Firefox 扩展开发
我正在修改篡改数据,这将允许我将其观察到的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果尝试在开始时声明变量,则您已经在内部定义了间隔
You have defined interval inside if try to declare your variable on the start
你做错了,每次你想设置新的间隔时,你应该先清除它
Your doing it wrong, each time you want to set the new interval you should clear it first
您将
间隔
存储在局部变量中;下次您尝试clearInterval
未定义的变量时,该值会在函数返回后丢失。将间隔存储在 ie 全局变量中:You're storing the
interval
in a local variable; the value is lost after the function returns, next time you attempt toclearInterval
an undefined variable. Store the interval in i.e. a global variable instead:当然,因为
interval
被定义为私有变量。它在toggleTimer
函数中定义,并在函数结束时被销毁。使用
interval = window.setInterval()
而不是var Interval = window.setInterval()
来定义稍后可供clearInterval
访问的全局变量。以下是 JavaScript 变量范围的一些示例。
var
用于定义当前范围内的变量。保留var
始终会创建或更改局部变量。Ofcourse, because
interval
is defined as a private variable. It is defined in thetoggleTimer
function and is destroyed when the function ends.Use
interval = window.setInterval()
instead ofvar interval = window.setInterval()
to define a global variable that is accessible later forclearInterval
.Below are some examples of the JavaScript variable scope.
var
is used to define a variable in the current scope. Leavingvar
always creates or changes a local variable.