How to sleep in firefox extension without using setTimeout?

I am trying to open a popup, wait for X seconds and close the popup.

(The use case sends a notification to webapp - but we can't just execute the GET request because it needs to be in the same session so we can use the login session)

I cannot use setTimeout as we cannot use it in add-ons / extensions

How can I get similar functionality without resorting to chewing on processor cycles, which obviously causes a noticeable lag?

+4
source share
2 answers

You can use nsITimer.

Below is a basic example, but you can find more information (including using Components.interfaces.nsITimer.TYPE_REPEATING_SLACK as an alternative to setInterval) on the corresponding documentation page at https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsITimer

 // we need an nsITimerCallback compatible interface for the callbacks. var event = { notify: function(timer) { alert("Fire!"); } } // Create the timer... var timer = Components.classes["@mozilla.org/timer;1"] .createInstance(Components.interfaces.nsITimer); // initialize it to call event.notify() once after exactly ten seconds. timer.initWithCallback(event,10000, Components.interfaces.nsITimer.TYPE_ONE_SHOT); 
+3
source

You can use the timers module provided by the SDK instead of nsITimer for the same type of setTimeout / setInterval functions provided in browsers

 let { setTimeout } = require('sdk/timers'); function openPopup () {} setTimeout(openPopup, 3000); 
+10
source

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


All Articles