| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | (function() { |
| 2 | var Timer; |
| 3 | |
| 4 | exports.Timer = Timer = (function() { |
| 5 | function Timer(func1) { |
| 6 | this.func = func1; |
| 7 | this.running = false; |
| 8 | this.id = null; |
| 9 | this._handler = (function(_this) { |
| 10 | return function() { |
| 11 | _this.running = false; |
| 12 | _this.id = null; |
| 13 | return _this.func(); |
| 14 | }; |
| 15 | })(this); |
| 16 | } |
| 17 | |
| 18 | Timer.prototype.start = function(timeout) { |
| 19 | if (this.running) { |
| 20 | clearTimeout(this.id); |
| 21 | } |
| 22 | this.id = setTimeout(this._handler, timeout); |
| 23 | return this.running = true; |
| 24 | }; |
| 25 | |
| 26 | Timer.prototype.stop = function() { |
| 27 | if (this.running) { |
| 28 | clearTimeout(this.id); |
| 29 | this.running = false; |
| 30 | return this.id = null; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | return Timer; |
| 35 | |
| 36 | })(); |
| 37 | |
| 38 | Timer.start = function(timeout, func) { |
| 39 | return setTimeout(func, timeout); |
| 40 | }; |
| 41 | |
| 42 | }).call(this); |