index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. Component({
  2. properties: {
  3. // 这里定义了innerText属性,属性值可以在组件使用时指定
  4. outTime: {
  5. type: Number,
  6. value: 0
  7. }
  8. },
  9. data: {
  10. day: null,
  11. hour: null,
  12. minute: null,
  13. second: null,
  14. timer: null
  15. },
  16. ready: function () {
  17. this.CaculateDate()
  18. },
  19. detached: function () {
  20. if (this.data.timer) {
  21. clearInterval(this.data.timer)
  22. }
  23. },
  24. methods: {
  25. CaculateDate: function () {
  26. var that = this
  27. var timerID = setInterval(function () {
  28. var leftTime = that.data.outTime - 1000
  29. var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10)
  30. var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10)
  31. var minutes = parseInt(leftTime / 1000 / 60 % 60, 10)
  32. var seconds = parseInt(leftTime / 1000 % 60, 10)
  33. if (leftTime > 0) {
  34. that.setData({
  35. outTime: leftTime
  36. })
  37. that.setData({
  38. day: days > 0 ? that.timeFormat(days) : null,
  39. hour: hours > 0 ? that.timeFormat(hours) : null,
  40. minute: minutes > 0 ? that.timeFormat(minutes) : null,
  41. second: seconds > 0 ? that.timeFormat(seconds) : null
  42. })
  43. } else { //结束
  44. clearInterval(that.data.timer)
  45. setTimeout(function () {
  46. that.triggerEvent('countDownDone', null)
  47. }, 2000)
  48. }
  49. }, 1000)
  50. this.setData({ timer: timerID })
  51. },
  52. timeFormat(param) { //小于10的格式化函数
  53. return param < 10 ? '0' + param : param
  54. }
  55. }
  56. })