如何向 Firefox 插件添加循环计时器?

发布于 2024-10-20 22:47:51 字数 661 浏览 5 评论 0原文

我正在尝试使用新的插件构建器预览 (https://builder.addons.mozilla.org/) 创建一个插件,并且我需要一个大约每 10 分钟运行一次的函数。我尝试过 setInterval 和 setTimeout,但它们都返回以下错误:(

    error: An exception occurred.
Traceback (most recent call last):
  File "resource://jid0-31njasqk3btmpa6paroepuybjn4-myaddon-lib/main.js", line 41, in 
    setTimeout(function() { timedCount(); }, 10000);
ReferenceError: setTimeout is not defined

当我尝试时,setTimeout 被 setInterval 替换。setTimeout 函数在我构建的类似网页中工作得很好。我只是让函数调用本身给出无限循环(听起来很愚蠢,应该有一个 while 循环,但它是在教程中;) 但现在我无法克服插件中的错误。

另外,如果你可以帮助我解析这个插件中的本地或远程页面(最好是远程的,但我可以让它解析本地主机上的 django 创建的页面),或者更好,只需告诉我如何使用 python ;)会很棒的。

谢谢!

I am trying to create an addon with the new addons builder preview (https://builder.addons.mozilla.org/), and I need a function to run about once every 10 minutes. I have tried both setInterval and setTimeout, but they both return the following error:

    error: An exception occurred.
Traceback (most recent call last):
  File "resource://jid0-31njasqk3btmpa6paroepuybjn4-myaddon-lib/main.js", line 41, in 
    setTimeout(function() { timedCount(); }, 10000);
ReferenceError: setTimeout is not defined

(with setTimeout being replaced with setInterval when I tried it. The setTimeout function worked great in the similar webpage that I built. I just had the function call itself to give an infinite loop (It sounds stupid, there should be a while loop, but it was in a tutorial;)
But now I can't get past that error in my addon.

Also, if you can help me parse a local or remote page in this addon (preferably remote, but I could make it parse a django-created page on localhost instead), or even better, just tell me how to use python ;) that would be great.

Thanks!

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

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

发布评论

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

评论(3

花开雨落又逢春i 2024-10-27 22:47:51

请注意,上面的内容已被弃用

var tmr = require('sdk/timers');

,现在已使用

please note that the above is deprecated

var tmr = require('sdk/timers');

is now used instead

梦里人 2024-10-27 22:47:51

使用计时器模块

var tmr = require('timer');
tmr.setInterval(timedCount, 10000); // no need for an anon function since you don't pass any arguments to your function nor capture anything in a closure

Use the timer module:

var tmr = require('timer');
tmr.setInterval(timedCount, 10000); // no need for an anon function since you don't pass any arguments to your function nor capture anything in a closure
孤寂小茶 2024-10-27 22:47:51

使用nsITimerhttps://developer.mozilla.org /en-US/docs/XPCOM_Interface_Reference/nsITimer

它不需要您使用不必要的 Jetpack SDK,以及额外的 require 功能;您可以像在 Mozilla 插件中进行其他 XPCOM 交互一样使用 Components.classes

Use nsITimer: https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsITimer

It doesn't require you to use the unnecessary Jetpack SDK, and the extra require function; you can use Components.classes like you do for other XPCOM interaction within Mozilla addons.

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