index.vue 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <uni-shadow-root class="vant-dist-count-down-index"><view class="van-count-down">
  3. <slot v-if="useSlot"></slot>
  4. <block v-else>{{ formattedTime }}</block>
  5. </view></uni-shadow-root>
  6. </template>
  7. <script>
  8. global['__wxRoute'] = 'vant/dist/count-down/index'
  9. import { VantComponent } from '../common/component';
  10. import { isSameSecond, parseFormat, parseTimeData } from './utils';
  11. function simpleTick(fn) {
  12. return setTimeout(fn, 30);
  13. }
  14. VantComponent({
  15. props: {
  16. useSlot: Boolean,
  17. millisecond: Boolean,
  18. time: {
  19. type: Number,
  20. observer: 'reset',
  21. },
  22. format: {
  23. type: String,
  24. value: 'HH:mm:ss',
  25. },
  26. autoStart: {
  27. type: Boolean,
  28. value: true,
  29. },
  30. },
  31. data: {
  32. timeData: parseTimeData(0),
  33. formattedTime: '0',
  34. },
  35. destroyed() {
  36. clearTimeout(this.tid);
  37. this.tid = null;
  38. },
  39. methods: {
  40. // 开始
  41. start() {
  42. if (this.counting) {
  43. return;
  44. }
  45. this.counting = true;
  46. this.endTime = Date.now() + this.remain;
  47. this.tick();
  48. },
  49. // 暂停
  50. pause() {
  51. this.counting = false;
  52. clearTimeout(this.tid);
  53. },
  54. // 重置
  55. reset() {
  56. this.pause();
  57. this.remain = this.data.time;
  58. this.setRemain(this.remain);
  59. if (this.data.autoStart) {
  60. this.start();
  61. }
  62. },
  63. tick() {
  64. if (this.data.millisecond) {
  65. this.microTick();
  66. }
  67. else {
  68. this.macroTick();
  69. }
  70. },
  71. microTick() {
  72. this.tid = simpleTick(() => {
  73. this.setRemain(this.getRemain());
  74. if (this.remain !== 0) {
  75. this.microTick();
  76. }
  77. });
  78. },
  79. macroTick() {
  80. this.tid = simpleTick(() => {
  81. const remain = this.getRemain();
  82. if (!isSameSecond(remain, this.remain) || remain === 0) {
  83. this.setRemain(remain);
  84. }
  85. if (this.remain !== 0) {
  86. this.macroTick();
  87. }
  88. });
  89. },
  90. getRemain() {
  91. return Math.max(this.endTime - Date.now(), 0);
  92. },
  93. setRemain(remain) {
  94. this.remain = remain;
  95. const timeData = parseTimeData(remain);
  96. if (this.data.useSlot) {
  97. this.$emit('change', timeData);
  98. }
  99. this.setData({
  100. formattedTime: parseFormat(this.data.format, timeData),
  101. });
  102. if (remain === 0) {
  103. this.pause();
  104. this.$emit('finish');
  105. }
  106. },
  107. },
  108. });
  109. export default global['__wxComponents']['vant/dist/count-down/index']
  110. </script>
  111. <style platform="mp-weixin">
  112. @import '../common/index.css';.van-count-down{color:#323233;color:var(--count-down-text-color,#323233);font-size:14px;font-size:var(--count-down-font-size,14px);line-height:20px;line-height:var(--count-down-line-height,20px)}
  113. </style>