CountdownTimer copy.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <MoveablePanel
  3. class="countdown-timer"
  4. :width="180"
  5. :height="110"
  6. :left="left"
  7. :top="top"
  8. >
  9. <div class="header">
  10. <span class="text-btn" @click="toggle()">{{ inTiming ? '暂停' : '开始'}}</span>
  11. <span class="text-btn" @click="reset()">重置</span>
  12. <span class="text-btn" @click="toggleCountdown()" :class="{ 'active': isCountdown }">倒计时</span>
  13. </div>
  14. <div class="content">
  15. <div class="timer">
  16. <input
  17. type="text"
  18. :value="fillDigit(minute, 2)"
  19. :maxlength="3" :disabled="inputEditable"
  20. @mousedown.stop
  21. @blur="$event => changeTime($event, 'minute')"
  22. @keydown.stop
  23. @keydown.enter.stop="$event => changeTime($event, 'minute')"
  24. >
  25. </div>
  26. <div class="colon">:</div>
  27. <div class="timer">
  28. <input
  29. type="text"
  30. :value="fillDigit(second, 2)"
  31. :maxlength="3" :disabled="inputEditable"
  32. @mousedown.stop
  33. @blur="$event => changeTime($event, 'second')"
  34. @keydown.stop
  35. @keydown.enter.stop="$event => changeTime($event, 'second')"
  36. >
  37. </div>
  38. </div>
  39. <div class="close-btn" @click="emit('close')"><IconClose class="icon" /></div>
  40. </MoveablePanel>
  41. </template>
  42. <script lang="ts" setup>
  43. import { computed, onUnmounted, ref } from 'vue'
  44. import { fillDigit } from '@/utils/common'
  45. import MoveablePanel from '@/components/MoveablePanel.vue'
  46. withDefaults(defineProps<{
  47. left?: number
  48. top?: number
  49. }>(), {
  50. left: 5,
  51. top: 5,
  52. })
  53. const emit = defineEmits<{
  54. (event: 'close'): void
  55. (event: 'timer-start', payload: { isCountdown: boolean; startAt: string; durationSec?: number }): void
  56. (event: 'timer-pause', payload: { pausedAt: string }): void
  57. (event: 'timer-reset'): void
  58. (event: 'timer-stop'): void
  59. (event: 'timer-finish'): void
  60. }>()
  61. const timer = ref<number | null>(null)
  62. const inTiming = ref(false)
  63. const isCountdown = ref(false)
  64. const time = ref(0)
  65. const minute = computed(() => Math.floor(time.value / 60))
  66. const second = computed(() => time.value % 60)
  67. const inputEditable = computed(() => {
  68. return !isCountdown.value || inTiming.value
  69. })
  70. const clearTimer = () => {
  71. if (timer.value !== null) {
  72. clearInterval(timer.value)
  73. }
  74. }
  75. onUnmounted(clearTimer)
  76. const pause = () => {
  77. clearTimer()
  78. inTiming.value = false
  79. emit('timer-pause', { pausedAt: new Date().toISOString() })
  80. }
  81. const reset = () => {
  82. clearTimer()
  83. inTiming.value = false
  84. if (isCountdown.value) time.value = 600
  85. else time.value = 0
  86. emit('timer-reset')
  87. }
  88. const start = () => {
  89. clearTimer()
  90. if (isCountdown.value) {
  91. timer.value = window.setInterval(() => {
  92. time.value = time.value - 1
  93. if (time.value <= 0) {
  94. time.value = 0
  95. clearTimer()
  96. inTiming.value = false
  97. emit('timer-finish')
  98. }
  99. }, 1000)
  100. }
  101. else {
  102. timer.value = window.setInterval(() => {
  103. time.value = time.value + 1
  104. if (time.value > 36000) pause()
  105. }, 1000)
  106. }
  107. inTiming.value = true
  108. emit('timer-start', { isCountdown: isCountdown.value, startAt: new Date().toISOString(), durationSec: isCountdown.value ? time.value : undefined })
  109. }
  110. const toggle = () => {
  111. if (inTiming.value) pause()
  112. else start()
  113. }
  114. const toggleCountdown = () => {
  115. isCountdown.value = !isCountdown.value
  116. reset()
  117. }
  118. onUnmounted(() => {
  119. emit('timer-stop')
  120. })
  121. const changeTime = (e: FocusEvent | KeyboardEvent, type: 'minute' | 'second') => {
  122. const inputRef = e.target as HTMLInputElement
  123. let value = inputRef.value
  124. const isNumber = /^(\d)+$/.test(value)
  125. if (isNumber) {
  126. if (type === 'second' && +value >= 60) value = '59'
  127. time.value = type === 'minute' ? (+value * 60 + second.value) : (+value + minute.value * 60)
  128. }
  129. else inputRef.value = type === 'minute' ? fillDigit(minute.value, 2) : fillDigit(second.value, 2)
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. .countdown-timer {
  134. user-select: none;
  135. }
  136. .header {
  137. height: 16px;
  138. font-size: 13px;
  139. margin-bottom: 16px;
  140. display: flex;
  141. align-items: center;
  142. .text-btn {
  143. margin-right: 8px;
  144. cursor: pointer;
  145. &:hover, &.active {
  146. color: $themeColor;
  147. }
  148. }
  149. }
  150. .content {
  151. display: flex;
  152. justify-content: space-between;
  153. padding: 0 5px;
  154. }
  155. .timer {
  156. width: 54px;
  157. height: 54px;
  158. border-radius: 50%;
  159. background-color: rgba($color: $themeColor, $alpha: .05);
  160. overflow: hidden;
  161. input {
  162. width: 100%;
  163. height: 100%;
  164. border: 0;
  165. outline: 0;
  166. background-color: transparent;
  167. text-align: center;
  168. font-size: 22px;
  169. }
  170. }
  171. .colon {
  172. height: 54px;
  173. line-height: 54px;
  174. font-size: 22px;
  175. }
  176. .icon-btn {
  177. width: 20px;
  178. height: 20px;
  179. display: flex;
  180. justify-content: center;
  181. align-items: center;
  182. cursor: pointer;
  183. }
  184. .pause, .play {
  185. font-size: 17px;
  186. }
  187. .reset {
  188. font-size: 12px;
  189. }
  190. .close-btn {
  191. position: absolute;
  192. top: 0;
  193. right: 0;
  194. padding: 10px;
  195. line-height: 1;
  196. cursor: pointer;
  197. }
  198. </style>