javascript - 如何在 Firefox 扩展中使用 nsITimer?

标签 javascript firefox-addon settimeout setinterval firefox4

我正在开发 Firefox 扩展,并希望使用计时器来控制每 60 秒发布一次数据。

以下内容放在主 .js 文件的初始化函数中:

var timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
timer.init(sendResults(true), 60000, 1);

但是,当我尝试运行它时,我在 Firefox 控制台中收到以下错误:

"Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER) [nsITimer.init]" nsresult: "0x80004003"...

等等。我做错了什么?

更新:

尽管最初使用 nsITimer 的问题仍然存在,但以下内容可满足我的需求:

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

解释其工作原理的有用链接(关于 setInterval/sendResults 的文档,以及“这个”问题的解决方案:

https://developer.mozilla.org/En/DOM/window.setTimeout https://developer.mozilla.org/En/Window.setInterval(不允许我发布超过两个超链接)

http://klevo.sk/javascript/javascripts-settimeout-and-how-to-use-it-with-your-methods/

最佳答案

nsITimer.init() 将观察者作为第一个参数。您可能想改用回调:

timer.initWithCallback(function() {sendResults(true); }, 60000, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);

但是 window.setInterval() 更易于使用 - 如果您有一个不会消失的窗口(关闭窗口会删除与其关联的所有间隔和超时)。

关于javascript - 如何在 Firefox 扩展中使用 nsITimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6256669/

相关文章:

xml - Firefox Addon - install.rdf - <em :description>. ..</em:description> 中的 "em"是什么意思

3d 数组的 Javascript "Cannot set property"

javascript - 我在从属性中提取 XML 数据时遇到问题,我不知道自己做错了什么

javascript - 如何动态安全地设置 XUL key ?

firefox-addon - 永久安装自定义的 Firefox 网络扩展 - 无需 mozilla 帐户或不稳定版本的 Firefox

php - 如何使每2分钟使用javascript重新加载我的php页面?

javascript - 将数组作为参数传递给 SetTimeout 回调

javascript - 在javascript中的另一个线程中运行代码

javascript - 向对象数组添加新属性

javascript - 为什么在javascript中不调用函数对象中的函数包装