1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
10 ////////////////////////////////////////////////////////////////////////////////
11 // returns timer object
12 function createTimer (aCallback, aDelay, oneshot) {
13 // create the timer object
14 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
15 // The timer object may be garbage collected before it fires, so we need to
16 // keep a reference to it alive (https://bugzil.la/647998).
17 // However, simply creating a closure over the timer object without using it
18 // may not be enough, as the timer might get optimized out of the closure
19 // scope (https://bugzil.la/640629#c9). To work around this, the timer object
20 // is explicitly stored as a property of the observer.
23 observe: function () {
26 if (observer.timer && oneshot) delete observer.timer;
31 timer.init(observer, aDelay, (oneshot ? Ci.nsITimer.TYPE_ONE_SHOT : Ci.nsITimer.TYPE_REPEATING_SLACK));
33 get active () { return !fired; },
34 get callback () { return aCallback; },
35 get oneShot () { return !!oneShot; },
36 get interval () { return aDelay; },
39 observer.timer.cancel();
40 delete observer.timer;
47 exports.oneShotTimer = function (aCallback, aDelay) { return createTimer(aCallback, aDelay, true); }
48 exports.intervalTimer = function (aCallback, aDelay) { return createTimer(aCallback, aDelay, false); }