countdown.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. Component({
  2. externalClasses: ['tui-countdown-class'],
  3. properties: {
  4. //数字框宽度
  5. width: {
  6. type: Number,
  7. value: 24
  8. },
  9. //数字框高度
  10. height: {
  11. type: Number,
  12. value: 24
  13. },
  14. //数字框border颜色
  15. bcolor: {
  16. type: String,
  17. value: "#333"
  18. },
  19. //数字框背景颜色
  20. bgcolor: {
  21. type: String,
  22. value: "#fff"
  23. },
  24. //数字框字体大小
  25. size: {
  26. type: Number,
  27. value: 24
  28. },
  29. //数字框字体颜色
  30. color: {
  31. type: String,
  32. value: "#333"
  33. },
  34. //是否缩放 0.9
  35. scale: {
  36. type: Boolean,
  37. value: false
  38. },
  39. //冒号大小
  40. colonSize: {
  41. type: Number,
  42. value: 28
  43. },
  44. //冒号颜色
  45. colonColor: {
  46. type: String,
  47. value: "#333"
  48. },
  49. //剩余时间 (单位:秒)
  50. time: {
  51. type: Number,
  52. value: 0,
  53. observer(val) {
  54. clearInterval(this.data.countdown)
  55. this.doLoop();
  56. }
  57. },
  58. //是否包含天
  59. days: {
  60. type: Boolean,
  61. value: false
  62. },
  63. //是否包含小时
  64. hours: {
  65. type: Boolean,
  66. value: true
  67. },
  68. //是否包含分钟
  69. minutes: {
  70. type: Boolean,
  71. value: true
  72. },
  73. //是否包含秒
  74. seconds: {
  75. type: Boolean,
  76. value: true
  77. },
  78. //是否展示为冒号,false为文字
  79. isColon: {
  80. type: Boolean,
  81. value: true
  82. }
  83. },
  84. data: {
  85. countdown: null,
  86. h: '00',
  87. i: '00',
  88. s: '00'
  89. },
  90. lifetimes: {
  91. detached: function() {
  92. clearInterval(this.data.countdown)
  93. this.setData({
  94. countdown: null
  95. })
  96. }
  97. },
  98. methods: {
  99. endOfTime() {
  100. clearInterval(this.data.countdown)
  101. this.setData({
  102. countdown: null
  103. })
  104. this.triggerEvent('end', {});
  105. },
  106. doLoop: function() {
  107. let seconds = this.data.time || 0
  108. this.countDown(seconds)
  109. this.setData({
  110. countdown: setInterval(() => {
  111. seconds--
  112. if (seconds < 0) {
  113. this.endOfTime()
  114. return
  115. }
  116. this.countDown(seconds)
  117. }, 1000)
  118. })
  119. },
  120. countDown(seconds) {
  121. let [day, hour, minute, second] = [0, 0, 0, 0]
  122. if (seconds > 0) {
  123. day = this.data.days ? Math.floor(seconds / (60 * 60 * 24)) : 0
  124. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  125. minute = Math.floor(seconds / 60) - (hour * 60) - (day * 24 * 60)
  126. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  127. } else {
  128. this.endOfTime()
  129. }
  130. hour = hour < 10 ? ('0' + hour) : hour
  131. minute = minute < 10 ? ('0' + minute) : minute
  132. second = second < 10 ? ('0' + second) : second
  133. this.setData({
  134. d: day,
  135. h: hour,
  136. i: minute,
  137. s: second
  138. })
  139. }
  140. }
  141. })