not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.9 KiB

  1. define(['events'], function(EventEmitter) {
  2. EventEmitter = EventEmitter.EventEmitter
  3. function Stopwatch(countDownMS, options) {
  4. STATUS = {
  5. STOPPED: 0,
  6. RUNNING: 1,
  7. COMPLETE: 2,
  8. }
  9. this.stoptime = 0
  10. this.refTime = 0
  11. this.tickTimer = 0
  12. this.almostDoneFired = false
  13. this.doneFired = false
  14. this.countDownMS = countDownMS || false
  15. this.ms = this.countDownMS || 0
  16. this._elapsedMS = 0
  17. this.state = STATUS.STOPPED
  18. if(!options) {options = {}}
  19. this.refreshRateMS = options.refreshRateMS || 50
  20. this.almostDoneMS = options.almostDoneMS || 10000
  21. this.reset(countDownMS)
  22. return this
  23. }
  24. Stopwatch.prototype = {
  25. start: function() {
  26. if (this.tickTimer) {
  27. clearInterval(this.tickTimer)
  28. }
  29. this.state = STATUS.RUNNING
  30. this.refTime = new Date().getTime()
  31. this.refTime -= this._elapsedMS
  32. var self = this
  33. this.tickTimer = setInterval(function(){self._updateTime()}, this.refreshRateMS)
  34. this._updateTime(this)
  35. },
  36. stop: function() {
  37. if(this.tickTimer) {
  38. clearInterval(this.tickTimer)
  39. }
  40. if(this.state === STATUS.RUNNING) {
  41. this.state = STATUS.STOPPED
  42. this._updateTime(this)
  43. this.emit('stop')
  44. this.emit('forcestop')
  45. }
  46. },
  47. reset: function(countDownMS) {
  48. this.stop()
  49. this.state = STATUS.STOPPED
  50. this.doneFired = false
  51. this.almostDoneFired = false
  52. this._elapsedMS = 0
  53. this.refTime = new Date().getTime()
  54. if(countDownMS) {
  55. this.countDownMS = countDownMS
  56. }
  57. this.ms = this.countDownMS || 0
  58. this.emit('time',{ms: this.ms})
  59. },
  60. startstop: function() {
  61. if(this.state === STATUS.STOPPED) {
  62. this.start()
  63. return true
  64. } else {
  65. this.stop()
  66. return false
  67. }
  68. },
  69. _updateTime: function() {
  70. var self = this
  71. if(self.countDownMS > 0) {
  72. self._timerCountdown(self)
  73. } else {
  74. self._stopwatchCountup(self)
  75. }
  76. },
  77. _timerCountdown: function() {
  78. var self = this
  79. var currentTime = new Date().getTime()
  80. self._elapsedMS = currentTime - self.refTime
  81. var remainingSeconds = self.countDownMS - self._elapsedMS
  82. if(remainingSeconds < 0) {
  83. remainingSeconds = 0
  84. }
  85. self.ms = remainingSeconds
  86. self.emit('time', {ms: self.ms})
  87. if(remainingSeconds <= 0) {
  88. self.stop()
  89. if(!self.doneFired) {
  90. self.doneFired = true
  91. self.state = STATUS.COMPLETE
  92. self.emit('done')
  93. }
  94. } else if (remainingSeconds < self.almostDoneMS) {
  95. if(!self.almostDoneFired) {
  96. self.almostDoneFired = true
  97. self.emit('almostdone')
  98. }
  99. }
  100. },
  101. _stopwatchCountup: function() {
  102. var self = this
  103. var currentTime = new Date().getTime()
  104. self._elapsedMS = currentTime - self.refTime
  105. self.ms = self._elapsedMS
  106. self.emit('time', {ms: self.ms})
  107. },
  108. onDone: function(cb) {
  109. this.on('done', cb)
  110. return this
  111. },
  112. onAlmostDone: function(cb) {
  113. this.on('almostDone', cb)
  114. return this
  115. },
  116. onTime: function(cb) {
  117. this.on('time', cb)
  118. return this
  119. },
  120. onStop: function(cb) {
  121. this.on('stop', cb)
  122. return this
  123. },
  124. }
  125. Object.assign(Stopwatch.prototype, EventEmitter.prototype)
  126. return Stopwatch
  127. })