u-count-down.vue 4.5 KB

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