cz-countdown2.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <view class="countdown fx-r fx-bc" :class="styleName == 'while' ? 'while' : ''">
  3. <view class="time" v-if="d!=0">{{d}}</view>
  4. <text class="text" v-if="d!=0">天</text>
  5. <view class="time" style="margin-left: 9rpx;">{{h<10?'0'+h:h}}</view>
  6. <text class="text">小时</text>
  7. <view class="time">{{m<10?'0'+m:m}}</view>
  8. <text class="text">分</text>
  9. <view class="time">{{s<10?'0'+s:s}}</view>
  10. <text class="text">秒</text>
  11. </view>
  12. </template>
  13. <script>
  14. export default{
  15. props:{
  16. startTime: {
  17. type: String,
  18. },
  19. endTime:{
  20. type: String,
  21. },
  22. styleName:{
  23. type : String,
  24. default(){
  25. return "while"
  26. }
  27. }
  28. },
  29. data(){
  30. return{
  31. timer:null,
  32. d:0,
  33. h:0,
  34. m:0,
  35. s:0
  36. }
  37. },
  38. mounted() {
  39. this.time()
  40. },
  41. beforeDestroy(){
  42. clearInterval(this.timer);
  43. this.timer = null;
  44. },
  45. computed:{
  46. sTime(){return this.startTime},
  47. eTime(){return this.endTime}
  48. },
  49. watch:{
  50. endTime(){
  51. clearInterval(this.timer);
  52. this.time()
  53. }
  54. },
  55. methods:{
  56. time(){
  57. let leftTime = this.GetDateDiff(this.sTime,this.eTime)
  58. this.getCountdownTime(leftTime)
  59. },
  60. //计算两个时间差
  61. GetDateDiff(startTime, endTime) {
  62. //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
  63. startTime = startTime.replace(/\-/g, "/");
  64. endTime = endTime.replace(/\-/g, "/");
  65. //将计算间隔类性字符转换为小写
  66. var sTime = new Date(startTime);
  67. var eTime = new Date(endTime);
  68. return parseInt((eTime.getTime() - sTime.getTime()) / 1000);
  69. },
  70. //计算活动结束时间
  71. getCountdownTime(leftTime){
  72. let time = leftTime
  73. if (time>0) {
  74. this.timer = setInterval(() => {
  75. if(time == 0){
  76. clearInterval(this.timer)
  77. this.h = 0
  78. this.m = 0
  79. this.s = 0
  80. }else{
  81. this.d = parseInt(leftTime/3600/24);
  82. this.h = parseInt((time/3600)%24);
  83. this.m = parseInt((time/60)%60);
  84. this.s = parseInt(time%60);
  85. time --
  86. }
  87. },1000)
  88. }
  89. }
  90. }
  91. }
  92. </script>
  93. <style>
  94. .countdown{font-size: 14px;color: red;}
  95. </style>