u-count-down.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 mpMixin from '../../libs/mixin/mpMixin.js';
  11. import mixin from '../../libs/mixin/mixin.js';
  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. methods: {
  57. init() {
  58. this.reset()
  59. },
  60. // 开始倒计时
  61. start() {
  62. if (this.runing) return
  63. // 标识为进行中
  64. this.runing = true
  65. // 结束时间戳 = 此刻时间戳 + 剩余的时间
  66. this.endTime = Date.now() + this.remainTime
  67. this.toTick()
  68. },
  69. // 根据是否展示毫秒,执行不同操作函数
  70. toTick() {
  71. if (this.millisecond) {
  72. this.microTick()
  73. } else {
  74. this.macroTick()
  75. }
  76. },
  77. macroTick() {
  78. this.clearTimeout()
  79. // 每隔一定时间,更新一遍定时器的值
  80. // 同时此定时器的作用也能带来毫秒级的更新
  81. this.timer = setTimeout(() => {
  82. // 获取剩余时间
  83. const remain = this.getRemainTime()
  84. // 重设剩余时间
  85. if (!isSameSecond(remain, this.remainTime) || remain === 0) {
  86. this.setRemainTime(remain)
  87. }
  88. // 如果剩余时间不为0,则继续检查更新倒计时
  89. if (this.remainTime !== 0) {
  90. this.macroTick()
  91. }
  92. }, 30)
  93. },
  94. microTick() {
  95. this.clearTimeout()
  96. this.timer = setTimeout(() => {
  97. this.setRemainTime(this.getRemainTime())
  98. if (this.remainTime !== 0) {
  99. this.microTick()
  100. }
  101. }, 50)
  102. },
  103. // 获取剩余的时间
  104. getRemainTime() {
  105. // 取最大值,防止出现小于0的剩余时间值
  106. return Math.max(this.endTime - Date.now(), 0)
  107. },
  108. // 设置剩余的时间
  109. setRemainTime(remain) {
  110. this.remainTime = remain
  111. // 根据剩余的毫秒时间,得出该有天,小时,分钟等的值,返回一个对象
  112. const timeData = parseTimeData(remain)
  113. this.$emit('change', timeData)
  114. // 得出格式化后的时间
  115. this.formattedTime = parseFormat(this.format, timeData)
  116. // 如果时间已到,停止倒计时
  117. if (remain <= 0) {
  118. this.pause()
  119. this.$emit('finish')
  120. }
  121. },
  122. // 重置倒计时
  123. reset() {
  124. this.pause()
  125. this.remainTime = this.time
  126. this.setRemainTime(this.remainTime)
  127. if (this.autoStart) {
  128. this.start()
  129. }
  130. },
  131. // 暂停倒计时
  132. pause() {
  133. this.runing = false;
  134. this.clearTimeout()
  135. },
  136. // 清空定时器
  137. clearTimeout() {
  138. clearTimeout(this.timer)
  139. this.timer = null
  140. }
  141. },
  142. beforeDestroy() {
  143. this.clearTimeout()
  144. }
  145. }
  146. </script>
  147. <style
  148. lang="scss"
  149. scoped
  150. >
  151. @import "../../libs/css/components.scss";
  152. $u-count-down-text-color:$u-content-color !default;
  153. $u-count-down-text-font-size:15px !default;
  154. $u-count-down-text-line-height:22px !default;
  155. .u-count-down {
  156. &__text {
  157. color: $u-count-down-text-color;
  158. font-size: $u-count-down-text-font-size;
  159. line-height: $u-count-down-text-line-height;
  160. }
  161. }
  162. </style>