1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/Timer.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,42 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +/** 1.11 + * JS module implementation of nsIDOMJSWindow.setTimeout and clearTimeout. 1.12 + */ 1.13 + 1.14 +this.EXPORTED_SYMBOLS = ["setTimeout", "clearTimeout"]; 1.15 + 1.16 +const Cc = Components.classes; 1.17 +const Ci = Components.interfaces; 1.18 +const Cu = Components.utils; 1.19 + 1.20 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.21 + 1.22 +// This gives us >=2^30 unique timer IDs, enough for 1 per ms for 12.4 days. 1.23 +let gNextTimeoutId = 1; // setTimeout must return a positive integer 1.24 + 1.25 +let gTimeoutTable = new Map(); // int -> nsITimer 1.26 + 1.27 +this.setTimeout = function setTimeout(aCallback, aMilliseconds) { 1.28 + let id = gNextTimeoutId++; 1.29 + let args = Array.slice(arguments, 2); 1.30 + let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); 1.31 + timer.initWithCallback(function setTimeout_timer() { 1.32 + gTimeoutTable.delete(id); 1.33 + aCallback.apply(null, args); 1.34 + }, aMilliseconds, timer.TYPE_ONE_SHOT); 1.35 + 1.36 + gTimeoutTable.set(id, timer); 1.37 + return id; 1.38 +} 1.39 + 1.40 +this.clearTimeout = function clearTimeout(aId) { 1.41 + if (gTimeoutTable.has(aId)) { 1.42 + gTimeoutTable.get(aId).cancel(); 1.43 + gTimeoutTable.delete(aId); 1.44 + } 1.45 +}