index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _utils = require("../utils");
  5. var _raf = require("../utils/dom/raf");
  6. var _utils2 = require("./utils");
  7. var _createNamespace = (0, _utils.createNamespace)('count-down'),
  8. createComponent = _createNamespace[0],
  9. bem = _createNamespace[1];
  10. var _default = createComponent({
  11. props: {
  12. millisecond: Boolean,
  13. time: {
  14. type: [Number, String],
  15. default: 0
  16. },
  17. format: {
  18. type: String,
  19. default: 'HH:mm:ss'
  20. },
  21. autoStart: {
  22. type: Boolean,
  23. default: true
  24. }
  25. },
  26. data: function data() {
  27. return {
  28. remain: 0
  29. };
  30. },
  31. computed: {
  32. timeData: function timeData() {
  33. return (0, _utils2.parseTimeData)(this.remain);
  34. },
  35. formattedTime: function formattedTime() {
  36. return (0, _utils2.parseFormat)(this.format, this.timeData);
  37. }
  38. },
  39. watch: {
  40. time: {
  41. immediate: true,
  42. handler: 'reset'
  43. }
  44. },
  45. activated: function activated() {
  46. if (this.keepAlivePaused) {
  47. this.counting = true;
  48. this.keepAlivePaused = false;
  49. this.tick();
  50. }
  51. },
  52. deactivated: function deactivated() {
  53. if (this.counting) {
  54. this.pause();
  55. this.keepAlivePaused = true;
  56. }
  57. },
  58. beforeDestroy: function beforeDestroy() {
  59. this.pause();
  60. },
  61. methods: {
  62. // @exposed-api
  63. start: function start() {
  64. if (this.counting) {
  65. return;
  66. }
  67. this.counting = true;
  68. this.endTime = Date.now() + this.remain;
  69. this.tick();
  70. },
  71. // @exposed-api
  72. pause: function pause() {
  73. this.counting = false;
  74. (0, _raf.cancelRaf)(this.rafId);
  75. },
  76. // @exposed-api
  77. reset: function reset() {
  78. this.pause();
  79. this.remain = +this.time;
  80. if (this.autoStart) {
  81. this.start();
  82. }
  83. },
  84. tick: function tick() {
  85. // should not start counting in server
  86. // see: https://github.com/vant-ui/vant/issues/7807
  87. if (!_utils.inBrowser) {
  88. return;
  89. }
  90. if (this.millisecond) {
  91. this.microTick();
  92. } else {
  93. this.macroTick();
  94. }
  95. },
  96. microTick: function microTick() {
  97. var _this = this;
  98. this.rafId = (0, _raf.raf)(function () {
  99. /* istanbul ignore if */
  100. // in case of call reset immediately after finish
  101. if (!_this.counting) {
  102. return;
  103. }
  104. _this.setRemain(_this.getRemain());
  105. if (_this.remain > 0) {
  106. _this.microTick();
  107. }
  108. });
  109. },
  110. macroTick: function macroTick() {
  111. var _this2 = this;
  112. this.rafId = (0, _raf.raf)(function () {
  113. /* istanbul ignore if */
  114. // in case of call reset immediately after finish
  115. if (!_this2.counting) {
  116. return;
  117. }
  118. var remain = _this2.getRemain();
  119. if (!(0, _utils2.isSameSecond)(remain, _this2.remain) || remain === 0) {
  120. _this2.setRemain(remain);
  121. }
  122. if (_this2.remain > 0) {
  123. _this2.macroTick();
  124. }
  125. });
  126. },
  127. getRemain: function getRemain() {
  128. return Math.max(this.endTime - Date.now(), 0);
  129. },
  130. setRemain: function setRemain(remain) {
  131. this.remain = remain;
  132. this.$emit('change', this.timeData);
  133. if (remain === 0) {
  134. this.pause();
  135. this.$emit('finish');
  136. }
  137. }
  138. },
  139. render: function render() {
  140. var h = arguments[0];
  141. return h("div", {
  142. "class": bem()
  143. }, [this.slots('default', this.timeData) || this.formattedTime]);
  144. }
  145. });
  146. exports.default = _default;