timer.js 875 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function() {
  2. var Timer;
  3. exports.Timer = Timer = (function() {
  4. function Timer(func1) {
  5. this.func = func1;
  6. this.running = false;
  7. this.id = null;
  8. this._handler = (function(_this) {
  9. return function() {
  10. _this.running = false;
  11. _this.id = null;
  12. return _this.func();
  13. };
  14. })(this);
  15. }
  16. Timer.prototype.start = function(timeout) {
  17. if (this.running) {
  18. clearTimeout(this.id);
  19. }
  20. this.id = setTimeout(this._handler, timeout);
  21. return this.running = true;
  22. };
  23. Timer.prototype.stop = function() {
  24. if (this.running) {
  25. clearTimeout(this.id);
  26. this.running = false;
  27. return this.id = null;
  28. }
  29. };
  30. return Timer;
  31. })();
  32. Timer.start = function(timeout, func) {
  33. return setTimeout(func, timeout);
  34. };
  35. }).call(this);