tm-countdown.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="tm-countdown d-inline-block text-size-n">
  3. <slot name="default" :timeData="{data:time_data,finish:isfinish}">{{text}}</slot>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * 倒计时
  9. * @description 倒计时。
  10. * @property {Number} time = [] 默认 10*1000,单位毫秒,倒计时的总时长。
  11. * @property {String} format = [] 默认 'DD天HH小时MM分SS秒',格式。如果只想要秒:SS秒。
  12. * @property {Boolean} autoStart = [] 默认 true,自动开始倒计时。
  13. * @property {Function} change 时间变化时触发。
  14. * @property {Function} finish 倒计时结束时触发。
  15. * @example <tm-button size="s"><tm-countdown format="SS秒" :time="6*1000">
  16. <template v-slot:default="{timeData}">
  17. {{timeData.finish?'结束':timeData.data.seconds}}
  18. </template>
  19. </tm-countdown></tm-button>
  20. */
  21. export default {
  22. name:"tm-countdown",
  23. props:{
  24. time:{
  25. type:Number,
  26. default:10 * 1000
  27. },
  28. format:{
  29. type:String,
  30. default:'DD天HH小时MM分SS秒'
  31. },
  32. autoStart:{
  33. type:Boolean,
  34. default:true
  35. }
  36. },
  37. data() {
  38. return {
  39. timid:null,
  40. now:0,
  41. time_data:{}
  42. };
  43. },
  44. computed:{
  45. text:function(){
  46. let minaox = this.formatTime(this.time - this.now);
  47. let ps = this.format;
  48. ps = ps.replace(/(DD)/g,minaox.day);
  49. ps = ps.replace(/(MM)/g,minaox.minutes);
  50. ps = ps.replace(/(HH)/g,minaox.hour);
  51. ps = ps.replace(/(SS)/g,minaox.seconds);
  52. return ps;
  53. },
  54. isfinish:function(){
  55. if(this.now == this.time) return true;
  56. return false;
  57. }
  58. },
  59. destroyed() {
  60. clearInterval(this.time);
  61. },
  62. mounted() {
  63. this.formatTime(this.time);
  64. if(this.autoStart){
  65. this.start();
  66. }
  67. },
  68. methods: {
  69. formatTime(my_time){
  70. var daysRound = Math.floor(my_time/1000/60/60/24);
  71. var hoursRound = Math.floor(my_time/1000/60/60%24);
  72. var minutesRound = Math.floor(my_time/1000/60%60);
  73. var secondsRound = Math.floor(my_time/1000%60);
  74. var millisecondRound = Math.floor(my_time % 1000);
  75. let time = {
  76. day:daysRound>9?daysRound:'0'+daysRound,//天
  77. hour:hoursRound>9?hoursRound:'0'+hoursRound,//小时,
  78. minutes:minutesRound>9?minutesRound:'0'+minutesRound,//分.
  79. seconds:secondsRound>9?secondsRound:'0'+secondsRound,//秒。
  80. millisecond:millisecondRound>9?millisecondRound:'0'+millisecondRound//毫秒。
  81. };
  82. this.time_data = time;
  83. return time;
  84. },
  85. // 开始或者继续。
  86. start() {
  87. let t = this;
  88. clearInterval(this.timid);
  89. this.timid = setInterval(()=>{
  90. let lst = t.now + 50;
  91. if(lst == t.time){
  92. t.$emit('finish')
  93. }
  94. if(lst > t.time){
  95. clearInterval(t.timid);
  96. return;
  97. }
  98. t.now =lst;
  99. t.$emit('change',t.time_data)
  100. },50)
  101. },
  102. // 停止,直接结束。
  103. stop(){
  104. clearInterval(this.timid);
  105. this.now = this.time;
  106. },
  107. // 暂停。
  108. pause(){
  109. clearInterval(this.timid);
  110. },
  111. // 重置。
  112. resinit(){
  113. clearInterval(this.timid);
  114. this.now = 0;
  115. }
  116. },
  117. }
  118. </script>
  119. <style lang="scss">
  120. </style>