timer.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Core(function(scope) {
  2. function Timer(root, options)
  3. {
  4. options = options || {};
  5. this.duration = options.duration;
  6. this.curTime = this.duration;
  7. this.$el = $(root);
  8. this.elements = {
  9. $value: "#value",
  10. $progress: "#progress"
  11. };
  12. this.proxies = [ "tick" ];
  13. this.__super__.constructor.call(this);
  14. this.$value.attr({x:7,y:26});
  15. this.$value.text(this.duration);
  16. defHeight = this.$progress.attr("height");
  17. }
  18. var defHeight;
  19. $.inherit(Timer, scope.Controller);
  20. $.extend(Timer.prototype, {
  21. from: function(time) {
  22. this.$value.text(this.curTime = Math.round(time / 1e3));
  23. },
  24. start: function() {
  25. this.$progress.move({
  26. attributeName: "height",
  27. to: "0",
  28. dur: this.curTime
  29. }),
  30. this.timerId = this.withDelay(this.tick, 1e3);
  31. },
  32. tick: function() {
  33. return this.paused
  34. ? void (this.timerId = this.withDelay(this.tick, 1e3))
  35. : void (this.curTime-- > 0 && (this.$value.text(this.curTime),
  36. this.timerId = this.withDelay(this.tick, 1e3))
  37. );
  38. },
  39. reset: function() {
  40. this.$progress.stop().attr({
  41. height: defHeight
  42. }),
  43. this.$value.text(this.curTime = this.duration),
  44. clearTimeout(this.timerId);
  45. },
  46. pause: function() {
  47. this.paused = !0, this.$progress.pause();
  48. },
  49. resume: function() {
  50. this.paused = !1, this.$progress.resume();
  51. }
  52. });
  53. scope.Timer = Timer;
  54. });