index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. var _component = require("../common/component");
  3. var _utils = require("./utils");
  4. var _my = require("../../__antmove/api/index.js")(my);
  5. var wx = _my;
  6. function simpleTick(fn) {
  7. return setTimeout(fn, 30);
  8. }
  9. (0, _component.VantComponent)({
  10. props: {
  11. useSlot: Boolean,
  12. millisecond: Boolean,
  13. time: {
  14. type: Number,
  15. observer: "reset"
  16. },
  17. format: {
  18. type: String,
  19. value: "HH:mm:ss"
  20. },
  21. autoStart: {
  22. type: Boolean,
  23. value: true
  24. }
  25. },
  26. data: {
  27. timeData: (0, _utils.parseTimeData)(0),
  28. formattedTime: "0"
  29. },
  30. destroyed: function destroyed() {
  31. clearTimeout(this.tid);
  32. this.tid = null;
  33. },
  34. methods: {
  35. // 开始
  36. start: function start() {
  37. if (this.counting) {
  38. return;
  39. }
  40. this.counting = true;
  41. this.endTime = Date.now() + this.remain;
  42. this.tick();
  43. },
  44. // 暂停
  45. pause: function pause() {
  46. this.counting = false;
  47. clearTimeout(this.tid);
  48. },
  49. // 重置
  50. reset: function reset() {
  51. this.pause();
  52. this.remain = this.data.time;
  53. this.setRemain(this.remain);
  54. if (this.data.autoStart) {
  55. this.start();
  56. }
  57. },
  58. tick: function tick() {
  59. if (this.data.millisecond) {
  60. this.microTick();
  61. } else {
  62. this.macroTick();
  63. }
  64. },
  65. microTick: function microTick() {
  66. var _this = this;
  67. this.tid = simpleTick(function () {
  68. _this.setRemain(_this.getRemain());
  69. if (_this.remain !== 0) {
  70. _this.microTick();
  71. }
  72. });
  73. },
  74. macroTick: function macroTick() {
  75. var _this2 = this;
  76. this.tid = simpleTick(function () {
  77. var remain = _this2.getRemain();
  78. if (!(0, _utils.isSameSecond)(remain, _this2.remain) || remain === 0) {
  79. _this2.setRemain(remain);
  80. }
  81. if (_this2.remain !== 0) {
  82. _this2.macroTick();
  83. }
  84. });
  85. },
  86. getRemain: function getRemain() {
  87. return Math.max(this.endTime - Date.now(), 0);
  88. },
  89. setRemain: function setRemain(remain) {
  90. var _this3 = this;
  91. this.remain = remain;
  92. var timeData = (0, _utils.parseTimeData)(remain);
  93. if (this.data.useSlot) {
  94. wx.nextTick(function () {
  95. _this3.$emit("change", timeData);
  96. });
  97. }
  98. this.setData({
  99. formattedTime: (0, _utils.parseFormat)(this.data.format, timeData)
  100. });
  101. if (remain === 0) {
  102. this.pause();
  103. this.$emit("finish");
  104. }
  105. }
  106. }
  107. });