index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var t = require("../../utils/timeFormat");
  2. Component({
  3. properties: {
  4. target: {
  5. type: String,
  6. observer: function(t) {
  7. this.init(t);
  8. }
  9. },
  10. showDay: Boolean,
  11. beginTime: String,
  12. callback: String,
  13. format: Array,
  14. clearTimer: {
  15. type: Boolean,
  16. observer: function (t) {
  17. t&&clearTimeout(this.data.timer);
  18. }
  19. }
  20. },
  21. externalClasses: ["countdown-class", "item-class"],
  22. data: {
  23. time: {
  24. day: "0",
  25. second: "00",
  26. minute: "00",
  27. hour: "00"
  28. },
  29. resultFormat: [],
  30. changeFormat: false,
  31. timeStamp: 0,
  32. timer: null
  33. },
  34. methods: {
  35. init: function(t) {
  36. var e = {
  37. day: "0",
  38. second: "00",
  39. minute: "00",
  40. hour: "00"
  41. };
  42. if (t - new Date().getTime() <= 0) return this.setData({
  43. time: e
  44. }), void this.triggerEvent("callback");
  45. this.interval(t);
  46. },
  47. interval: function(e) {
  48. var a = this,
  49. i = e - new Date().getTime();
  50. if (i <= 0) return clearTimeout(this.data.timer), this.triggerEvent("callback"),
  51. void this.setData({
  52. time: {
  53. day: "0",
  54. second: "00",
  55. minute: "00",
  56. hour: "00"
  57. }
  58. });
  59. var r = Math.ceil(i / 1000),
  60. n = parseInt(r / 86400),
  61. o = r % 86400,
  62. s = (0, t.formatNumber)(parseInt(o / 3600));
  63. o %= 3600;
  64. var m = {
  65. day: n,
  66. hour: s,
  67. minute: (0, t.formatNumber)(parseInt(o / 60)),
  68. second: (0, t.formatNumber)(o % 60)
  69. };
  70. this.setData({
  71. time: m
  72. }), this.data.timer = setTimeout(function() {
  73. a.interval(e);
  74. }, 1000);
  75. }
  76. },
  77. detached: function() {
  78. clearTimeout(this.data.timer);
  79. }
  80. });