summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/Timer.jsm
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/modules/Timer.jsm
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/modules/Timer.jsm')
-rw-r--r--toolkit/modules/Timer.jsm54
1 files changed, 54 insertions, 0 deletions
diff --git a/toolkit/modules/Timer.jsm b/toolkit/modules/Timer.jsm
new file mode 100644
index 000000000..caef68eac
--- /dev/null
+++ b/toolkit/modules/Timer.jsm
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/**
+ * JS module implementation of setTimeout and clearTimeout.
+ */
+
+this.EXPORTED_SYMBOLS = ["setTimeout", "clearTimeout", "setInterval", "clearInterval"];
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+// This gives us >=2^30 unique timer IDs, enough for 1 per ms for 12.4 days.
+var gNextId = 1; // setTimeout and setInterval must return a positive integer
+
+var gTimerTable = new Map(); // int -> nsITimer
+
+this.setTimeout = function setTimeout(aCallback, aMilliseconds) {
+ let id = gNextId++;
+ let args = Array.slice(arguments, 2);
+ let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+ timer.initWithCallback(function setTimeout_timer() {
+ gTimerTable.delete(id);
+ aCallback.apply(null, args);
+ }, aMilliseconds, timer.TYPE_ONE_SHOT);
+
+ gTimerTable.set(id, timer);
+ return id;
+}
+
+this.setInterval = function setInterval(aCallback, aMilliseconds) {
+ let id = gNextId++;
+ let args = Array.slice(arguments, 2);
+ let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+ timer.initWithCallback(function setInterval_timer() {
+ aCallback.apply(null, args);
+ }, aMilliseconds, timer.TYPE_REPEATING_SLACK);
+
+ gTimerTable.set(id, timer);
+ return id;
+}
+
+this.clearInterval = this.clearTimeout = function clearTimeout(aId) {
+ if (gTimerTable.has(aId)) {
+ gTimerTable.get(aId).cancel();
+ gTimerTable.delete(aId);
+ }
+}