index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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: Boolean
  15. },
  16. externalClasses: ["countdown-class", "item-class"],
  17. data: {
  18. time: {
  19. day: "0",
  20. second: "00",
  21. minute: "00",
  22. hour: "00"
  23. },
  24. resultFormat: [],
  25. changeFormat: false,
  26. timeStamp: 0,
  27. timer: null
  28. },
  29. methods: {
  30. init: function(t) {
  31. var e = {
  32. day: "0",
  33. second: "00",
  34. minute: "00",
  35. hour: "00"
  36. };
  37. if (t - new Date().getTime() <= 0) return this.setData({
  38. time: e
  39. }), void this.triggerEvent("callback");
  40. this.interval(t);
  41. },
  42. interval: function(e) {
  43. var a = this,
  44. i = e - new Date().getTime();
  45. if (i <= 0) return clearTimeout(this.data.timer), this.triggerEvent("callback"),
  46. void this.setData({
  47. time: {
  48. day: "0",
  49. second: "00",
  50. minute: "00",
  51. hour: "00"
  52. }
  53. });
  54. var r = Math.ceil(i / 1000),
  55. n = parseInt(r / 86400),
  56. o = r % 86400,
  57. s = (0, t.formatNumber)(parseInt(o / 3600));
  58. o %= 3600;
  59. var m = {
  60. day: n,
  61. hour: s,
  62. minute: (0, t.formatNumber)(parseInt(o / 60)),
  63. second: (0, t.formatNumber)(o % 60)
  64. };
  65. this.setData({
  66. time: m
  67. }), this.data.timer = setTimeout(function() {
  68. a.interval(e);
  69. }, 1000);
  70. }
  71. },
  72. detached: function() {
  73. clearTimeout(this.data.timer);
  74. }
  75. });