u-count-down.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="u-count-down">
  3. <slot>
  4. <text class="u-count-down__text">{{ formattedTime }}</text>
  5. </slot>
  6. </view>
  7. </template>
  8. <script>
  9. import props from './props.js';
  10. import mixin from '../../libs/mixin/mixin'
  11. import mpMixin from '../../libs/mixin/mpMixin';
  12. import {
  13. isSameSecond,
  14. parseFormat,
  15. parseTimeData
  16. } from './utils';
  17. /**
  18. * u-count-down 倒计时
  19. * @description 该组件一般使用于某个活动的截止时间上,通过数字的变化,给用户明确的时间感受,提示用户进行某一个行为操作。
  20. * @tutorial https://uviewui.com/components/countDown.html
  21. * @property {String | Number} time 倒计时时长,单位ms (默认 0 )
  22. * @property {String} format 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 (默认 'HH:mm:ss' )
  23. * @property {Boolean} autoStart 是否自动开始倒计时 (默认 true )
  24. * @property {Boolean} millisecond 是否展示毫秒倒计时 (默认 false )
  25. * @event {Function} finish 倒计时结束时触发
  26. * @event {Function} change 倒计时变化时触发
  27. * @event {Function} start 开始倒计时
  28. * @event {Function} pause 暂停倒计时
  29. * @event {Function} reset 重设倒计时,若 auto-start 为 true,重设后会自动开始倒计时
  30. * @example <u-count-down :time="time"></u-count-down>
  31. */
  32. export default {
  33. name: 'u-count-down',
  34. mixins: [mpMixin, mixin, props],
  35. data() {
  36. return {
  37. timer: null,
  38. // 各单位(天,时,分等)剩余时间
  39. timeData: parseTimeData(0),
  40. // 格式化后的时间,如"03:23:21"
  41. formattedTime: '0',
  42. // 倒计时是否正在进行中
  43. runing: false,
  44. endTime: 0, // 结束的毫秒时间戳
  45. remainTime: 0, // 剩余的毫秒时间
  46. }
  47. },
  48. watch: {
  49. time(n) {
  50. this.reset()
  51. }
  52. },
  53. mounted() {
  54. this.init()
  55. },
  56. // #ifdef VUE3
  57. emits: ["finish", "change"],
  58. // #endif
  59. methods: {
  60. init() {
  61. this.reset()
  62. },
  63. // 开始倒计时
  64. start() {
  65. if (this.runing) return
  66. // 标识为进行中
  67. this.runing = true
  68. // 结束时间戳 = 此刻时间戳 + 剩余的时间
  69. this.endTime = Date.now() + this.remainTime
  70. this.toTick()
  71. },
  72. // 根据是否展示毫秒,执行不同操作函数
  73. toTick() {
  74. if (this.millisecond) {
  75. this.microTick()
  76. } else {
  77. this.macroTick()
  78. }
  79. },
  80. macroTick() {
  81. this.clearTimeout()
  82. // 每隔一定时间,更新一遍定时器的值
  83. // 同时此定时器的作用也能带来毫秒级的更新
  84. this.timer = setTimeout(() => {
  85. // 获取剩余时间
  86. const remain = this.getRemainTime()
  87. // 重设剩余时间
  88. if (!isSameSecond(remain, this.remainTime) || remain === 0) {
  89. this.setRemainTime(remain)
  90. }
  91. // 如果剩余时间不为0,则继续检查更新倒计时
  92. if (this.remainTime !== 0) {
  93. this.macroTick()
  94. }
  95. }, 30)
  96. },
  97. microTick() {
  98. this.clearTimeout()
  99. this.timer = setTimeout(() => {
  100. this.setRemainTime(this.getRemainTime())
  101. if (this.remainTime !== 0) {
  102. this.microTick()
  103. }
  104. }, 50)
  105. },
  106. // 获取剩余的时间
  107. getRemainTime() {
  108. // 取最大值,防止出现小于0的剩余时间值
  109. return Math.max(this.endTime - Date.now(), 0)
  110. },
  111. // 设置剩余的时间
  112. setRemainTime(remain) {
  113. this.remainTime = remain
  114. // 根据剩余的毫秒时间,得出该有天,小时,分钟等的值,返回一个对象
  115. const timeData = parseTimeData(remain)
  116. this.$emit('change', timeData)
  117. // 得出格式化后的时间
  118. this.formattedTime = parseFormat(this.format, timeData)
  119. // 如果时间已到,停止倒计时
  120. if (remain <= 0) {
  121. this.pause()
  122. this.$emit('finish')
  123. }
  124. },
  125. // 重置倒计时
  126. reset() {
  127. this.pause()
  128. this.remainTime = Number(this.time)
  129. this.setRemainTime(this.remainTime)
  130. if (this.autoStart) {
  131. this.start()
  132. }
  133. },
  134. // 暂停倒计时
  135. pause() {
  136. this.runing = false;
  137. this.clearTimeout()
  138. },
  139. // 清空定时器
  140. clearTimeout() {
  141. clearTimeout(this.timer)
  142. this.timer = null
  143. }
  144. },
  145. // #ifdef VUE2
  146. beforeDestroy() {
  147. this.clearTimeout()
  148. }
  149. // #endif
  150. // #ifdef VUE3
  151. beforeUnmount() {
  152. this.clearTimeout()
  153. }
  154. // #endif
  155. }
  156. </script>
  157. <style
  158. lang="scss"
  159. scoped
  160. >
  161. @import "../../libs/css/components.scss";
  162. $u-count-down-text-color:$u-content-color !default;
  163. $u-count-down-text-font-size:15px !default;
  164. $u-count-down-text-line-height:22px !default;
  165. .u-count-down {
  166. &__text {
  167. color: $u-count-down-text-color;
  168. font-size: $u-count-down-text-font-size;
  169. line-height: $u-count-down-text-line-height;
  170. }
  171. }
  172. </style>