index.vue 3.4 KB

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