count-down.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { isSameSecond, parseFormat, parseTimeData } from './utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-count-down`;
  13. const simpleTick = function (fn) {
  14. return setTimeout(fn, 30);
  15. };
  16. let CountDown = class CountDown extends SuperComponent {
  17. constructor() {
  18. super(...arguments);
  19. this.externalClasses = ['t-class'];
  20. this.properties = props;
  21. this.observers = {
  22. time() {
  23. this.reset();
  24. },
  25. };
  26. this.data = {
  27. classPrefix: name,
  28. timeData: parseTimeData(0),
  29. formattedTime: '0',
  30. };
  31. this.tid = null;
  32. }
  33. detached() {
  34. this.destroyed();
  35. }
  36. destroyed() {
  37. if (this.tid) {
  38. clearTimeout(this.tid);
  39. this.tid = null;
  40. }
  41. }
  42. // 开始
  43. start() {
  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() {
  53. this.counting = false;
  54. this.tid && clearTimeout(this.tid);
  55. }
  56. // 重置
  57. reset() {
  58. this.pause();
  59. this.remain = this.properties.time;
  60. this.setRemain(this.remain);
  61. if (this.properties.autoStart) {
  62. this.start();
  63. }
  64. }
  65. tick() {
  66. if (this.properties.millisecond) {
  67. this.microTick();
  68. }
  69. else {
  70. this.macroTick();
  71. }
  72. }
  73. microTick() {
  74. this.tid = simpleTick(() => {
  75. this.setRemain(this.getRemain());
  76. if (this.remain !== 0) {
  77. this.microTick();
  78. }
  79. });
  80. }
  81. macroTick() {
  82. this.tid = simpleTick(() => {
  83. const remain = this.getRemain();
  84. if (!isSameSecond(remain, this.remain) || remain === 0) {
  85. this.setRemain(remain);
  86. }
  87. if (this.remain !== 0) {
  88. this.macroTick();
  89. }
  90. });
  91. }
  92. getRemain() {
  93. return Math.max(this.endTime - Date.now(), 0);
  94. }
  95. setRemain(remain) {
  96. this.remain = remain;
  97. const timeData = parseTimeData(remain);
  98. this.triggerEvent('change', timeData);
  99. const { timeText } = parseFormat(remain, this.properties.format);
  100. this.setData({
  101. timeData,
  102. formattedTime: timeText.replace(/:/g, ' : '),
  103. });
  104. if (remain === 0) {
  105. this.pause();
  106. this.triggerEvent('finish');
  107. }
  108. }
  109. };
  110. CountDown = __decorate([
  111. wxComponent()
  112. ], CountDown);
  113. export default CountDown;
  114. //# sourceMappingURL=count-down.js.map