Guest User

Untitled

a guest
Aug 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. <script>
  2. // 未来的终止时间 2018.10.10 23:59:59
  3. const futureDate = new Date(2018, 10, 10, 23, 59, 59)
  4.  
  5. export default {
  6. data () {
  7. return {
  8. time: {
  9. day: '69', // String
  10. hour: '59', // String
  11. minute: '59', // String
  12. seconds: '59' // String
  13. }
  14. }
  15. },
  16. mounted () {
  17. this.countDownComputer() // 初始化倒计时
  18. this.countDownMachine() // 开始倒计时
  19. },
  20. methods: {
  21. addZero: num => num.toString().length === 1 ? `0${num}` : num,
  22. formateCountDown () {
  23. const time = this.time
  24. return `${time.day} 天 ${time.hour} 时 ${time.minute} 分 ${time.seconds} 秒`
  25. },
  26. // 倒计时计算器
  27. countDownComputer () {
  28. let nowTime = Date.now()
  29. const secondMinuend = 1000
  30. const minMinuend = 1000 * 60
  31. const hourMinuend = 1000 * 60 * 60
  32. const dayMinuend = 1000 * 60 * 60 * 24
  33. let countDownSeconds = futureDate - nowTime
  34. const day = (countDownSeconds / dayMinuend).toFixed()
  35. const hour = (countDownSeconds % dayMinuend / hourMinuend).toFixed()
  36. const minute = (countDownSeconds % dayMinuend % hourMinuend / minMinuend).toFixed()
  37. const seconds = (countDownSeconds % dayMinuend % hourMinuend % minMinuend / secondMinuend).toFixed()
  38. if (day !== this.time.day) this.time.day = this.addZero(day)
  39. if (hour !== this.time.hour) this.time.hour = this.addZero(hour)
  40. if (minute !== this.time.minute) this.time.minute = this.addZero(minute)
  41. if (seconds !== this.time.seconds) this.time.seconds = this.addZero(seconds)
  42. },
  43. // 倒计时启动器
  44. countDownMachine () {
  45. const vm = this
  46. const perMinute = 1000
  47. setInterval(() => vm.countDownComputer(), perMinute)
  48. }
  49. }
  50. }
  51. </script>
Add Comment
Please, Sign In to add comment