clearInterval()不停止setInterval() - Firefox扩展开发 [英] clearInterval() is not stopping setInterval() - Firefox Extension Development

查看:207
本文介绍了clearInterval()不停止setInterval() - Firefox扩展开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改篡改数据,这将允许我发送它观察到的HTTP请求/响应到服务器。到目前为止,该功能已正确实施。下一步是自动化这个过程,我希望使用一个类型为checkbox的toolbarmenu按钮来打开和关闭这个功能。



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

 < toolbarbutton id =tamper.autosendlabel =& tamper.toolbar .autosend; type =checkboxoncommand =oTamper.toggleTimer();/> 

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

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

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




$ b $ p $使用consoleService我看到'checked 的确是对的。我相信问题在于我如何调用clearInterval,但我不确定如何补救。



任何帮助都非常感谢!

解决方案

如果您尝试在开始时声明您的变量, p> 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);
}
}


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!

解决方案

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

这篇关于clearInterval()不停止setInterval() - Firefox扩展开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆