count-down.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. let CountDown = class CountDown extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`];
  17. this.properties = props;
  18. this.observers = {
  19. time() {
  20. this.reset();
  21. },
  22. };
  23. this.data = {
  24. classPrefix: name,
  25. timeData: parseTimeData(0),
  26. formattedTime: '0',
  27. };
  28. this.timeoutId = null;
  29. this.lifetimes = {
  30. detached() {
  31. if (this.timeoutId) {
  32. clearTimeout(this.timeoutId);
  33. this.timeoutId = null;
  34. }
  35. },
  36. };
  37. this.methods = {
  38. start() {
  39. if (this.counting) {
  40. return;
  41. }
  42. this.counting = true;
  43. this.endTime = Date.now() + this.remain;
  44. this.doCount();
  45. },
  46. pause() {
  47. this.counting = false;
  48. this.timeoutId && clearTimeout(this.timeoutId);
  49. },
  50. reset() {
  51. this.pause();
  52. this.remain = this.properties.time;
  53. this.updateTime(this.remain);
  54. if (this.properties.autoStart) {
  55. this.start();
  56. }
  57. },
  58. getTime() {
  59. return Math.max(this.endTime - Date.now(), 0);
  60. },
  61. updateTime(remain) {
  62. this.remain = remain;
  63. const timeData = parseTimeData(remain);
  64. this.triggerEvent('change', timeData);
  65. const { timeText } = parseFormat(remain, this.properties.format);
  66. this.setData({
  67. timeData,
  68. formattedTime: timeText.replace(/:/g, ' : '),
  69. });
  70. if (remain === 0) {
  71. this.pause();
  72. this.triggerEvent('finish');
  73. }
  74. },
  75. doCount() {
  76. this.timeoutId = setTimeout(() => {
  77. const time = this.getTime();
  78. if (this.properties.millisecond) {
  79. this.updateTime(time);
  80. }
  81. else if (!isSameSecond(time, this.remain) || time === 0) {
  82. this.updateTime(time);
  83. }
  84. if (time !== 0) {
  85. this.doCount();
  86. }
  87. }, 33);
  88. },
  89. };
  90. }
  91. };
  92. CountDown = __decorate([
  93. wxComponent()
  94. ], CountDown);
  95. export default CountDown;