How to sleep in a firefox extension without using setTimeout?
firefox, firefox-addon, firefox-addon-sdk, javascript
Solution
You can use nsITimer.
A basic example is below but you can find more information (including the use of Components.interfaces.nsITimer.TYPE_REPEATING_SLACK as an alternative to setInterval) on the relevant 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);
Problem
I am trying to open a pop-up window, wait X seconds and then close the popup window. (The use case is sending a notification to a webapp - but we can't just do a GET request as it needs to be in the same session so we can use the login session) I can't use `setTimeout` as we can't use it in add-ons/extensions How can I get similar functionality without resorting to chewing up CPU cycles, which obviously causes a noticeable lag?