4

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!

Just a student
  • 10,560
  • 2
  • 41
  • 69
Crazedpsyc
  • 43
  • 1
  • 3

3 Answers3

9

please note that the above is deprecated

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

is now used instead

Tubelight
  • 313
  • 3
  • 7
5

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
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

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.

Jez
  • 27,951
  • 32
  • 136
  • 233