Timer.js 699 B

123456789101112131415161718192021222324252627282930313233
  1. var Timers = require('timers');
  2. module.exports = Timer;
  3. function Timer(object) {
  4. this._object = object;
  5. this._timeout = null;
  6. }
  7. Timer.prototype.active = function active() {
  8. if (this._timeout) {
  9. if (this._timeout.refresh) {
  10. this._timeout.refresh();
  11. } else {
  12. Timers.active(this._timeout);
  13. }
  14. }
  15. };
  16. Timer.prototype.start = function start(msecs) {
  17. this.stop();
  18. this._timeout = Timers.setTimeout(this._onTimeout.bind(this), msecs);
  19. };
  20. Timer.prototype.stop = function stop() {
  21. if (this._timeout) {
  22. Timers.clearTimeout(this._timeout);
  23. this._timeout = null;
  24. }
  25. };
  26. Timer.prototype._onTimeout = function _onTimeout() {
  27. return this._object._onTimeout();
  28. };