How to add recurring timer to Firefox addon?

I am trying to create an addon with a new add-on previewer (https://builder.addons.mozilla.org/) and I need a function to run about once every 10 minutes. I 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 replacing setTimeout with setInterval when I tried it. The network function setTimeout worked fine on the same web page I built. I just had a function call for an infinite loop (that sounds silly, it should be a loop time, but it was in the textbook;) But now I can not get through this error in my addon.

Also, if you can help me parse the local or remote page in this addon (preferably remotely, but I can get it to parse the created django page on localhost), or even better, just tell me how to use python;), which would be great.

Thanks!

+4
source share
3 answers

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 
+5
source

note that the above is deprecated

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

now used instead

+9
source

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

This does not require the use of the unnecessary Jetpack SDK and the additional require function; you can use Components.classes , as for other XPCOM interactions in Mozilla add-ons.

+2
source

Source: https://habr.com/ru/post/1343022/


All Articles