2013-02-25 23:08:33 +04:00
|
|
|
/* 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 nsIDOMJSWindow.setTimeout and clearTimeout.
|
|
|
|
*/
|
|
|
|
|
2015-03-28 01:38:17 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["setTimeout", "clearTimeout", "setInterval", "clearInterval"];
|
2013-02-25 23:08:33 +04:00
|
|
|
|
|
|
|
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.
|
2015-03-28 01:38:17 +03:00
|
|
|
let gNextId = 1; // setTimeout and setInterval must return a positive integer
|
2013-02-25 23:08:33 +04:00
|
|
|
|
2015-03-28 01:38:17 +03:00
|
|
|
let gTimerTable = new Map(); // int -> nsITimer
|
2013-02-25 23:08:33 +04:00
|
|
|
|
|
|
|
this.setTimeout = function setTimeout(aCallback, aMilliseconds) {
|
2015-03-28 01:38:17 +03:00
|
|
|
let id = gNextId++;
|
2013-03-02 18:46:40 +04:00
|
|
|
let args = Array.slice(arguments, 2);
|
2013-02-25 23:08:33 +04:00
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
timer.initWithCallback(function setTimeout_timer() {
|
2015-03-28 01:38:17 +03:00
|
|
|
gTimerTable.delete(id);
|
2013-03-02 18:46:40 +04:00
|
|
|
aCallback.apply(null, args);
|
2013-02-25 23:08:33 +04:00
|
|
|
}, aMilliseconds, timer.TYPE_ONE_SHOT);
|
|
|
|
|
2015-03-28 01:38:17 +03:00
|
|
|
gTimerTable.set(id, timer);
|
2013-02-25 23:08:33 +04:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-03-28 01:38:17 +03:00
|
|
|
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);
|
2013-02-25 23:08:33 +04:00
|
|
|
}
|
|
|
|
}
|