Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
_ff-addon-snippet-xpcomSetTimeout - How to use setTimeout from DOM-less scope.
var gTempTimers = {}; // hold temporary timers, when first arg is not set for xpcomSetTimeout
function xpcomSetTimeout(aNsiTimer, aDelayTimerMS, aTimerCallback) {
var timer;
if (!aNsiTimer) {
var timerid = Date.now();
gTempTimers[timerid] = Cc['@mozilla.org/timer;1'].createInstance(Ci.nsITimer);
timer = gTempTimers[timerid];
} else {
timer = aNsiTimer;
}
timer.initWithCallback({
notify: function() {
aTimerCallback();
if (!aNsiTimer) {
delete gTempTimers[timerid];
}
}
}, aDelayTimerMS, Ci.nsITimer.TYPE_ONE_SHOT);
}
@Noitidart

This comment has been minimized.

Copy link
Owner Author

@Noitidart Noitidart commented Jan 14, 2016

README

Rev1

  • Works
@Noitidart

This comment has been minimized.

Copy link
Owner Author

@Noitidart Noitidart commented Aug 21, 2016

Stick in shutdown:

for (var timerid in gTempTimers) {
  var timer = gTempTimers[timerid];
  timer.cancel();
delete gTempTimers[timerid];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment