Vue倒计时组件 “时分秒”

<template>
  <div class="countDownBox">
    <div><span class="timeSpan">{{h}}</span>:<span class="timeSpan">{{m}}</span>:<span class="timeSpan">{{s}}</span></div>
  </div>
</template>
<script>
export default {
  // time: '12:30',  notTimes: '10:48:07.911'
  props: ['time', 'notTimes'],
  data () {
    return {
      d: '00',
      h: '00',
      m: '00',
      s: '00',
      difference: 0,
      timess: null,
      flag: false
    }
  },
  watch: {
    time (val) {
      this.startTime()
      this.flag = false
      this.timeDown()
    }
  },
  created () {
    this.startTime()
    this.timeDown()
  },
  methods: {
    // 初始化时间
    startTime () {
      let nowTime = new Date()
      let Y = nowTime.getFullYear()
      let M = nowTime.getMonth() + 1
      let D = nowTime.getDate()
      let time = Y + '/' + M + '/' + D + ' ' + this.time + ':00'
      let nowTime2 = Y + '/' + M + '/' + D + ' ' + this.notTimes.split('.')[0]
      let beginTime = new Date(nowTime2)
      let endTime = new Date(time)
      this.difference = parseInt((endTime.getTime() - beginTime.getTime()) / 1000)
      if (this.difference === 0 || this.difference === -1) {
        this.$emit('timeEnd')
      }
    },
    // 倒计时
    timeDown () {
      // 切换清除之前的定时器
      clearInterval(this.timess)
      this.timess = setInterval(() => {
        if (this.flag === true) {
          clearInterval(this.timess)
        }
        this.difference = this.difference - 1
        if (this.difference > 0) {
          this.d = parseInt(this.difference / (60 * 60 * 24))
          this.h = this.format(parseInt(this.difference / (60 * 60) % 24))
          this.m = this.format(parseInt((this.difference / 60) % 60))
          this.s = this.format(parseInt(this.difference % 60))
        } else {
          this.d = '00'
          this.h = '00'
          this.m = '00'
          this.s = '00'
          this.flag = true
        }
      }, 1000)
    },
    // 时间格式
    format (time) {
      if (time >= 10) {
        return time
      } else {
        return `0${time}`
      }
    }
  }
}
</script>
<style lang="less" scoped>
.countDownBox{
  font-size: 0.28rem;
  padding-right: 0.1rem;
  .homeTime{
    color: #EA564A;
    float: left;
  }
  .DetailTime{
    text-align: center;
    color: #EA564A;
    .timeSpan{
      color: #fff;
      width:0.4rem;
      height:.46rem;
      font-size: 0.28rem;
      background:rgba(234,86,74,1);
      border-radius:6px;
      display: inline-block;
      line-height: 0.46rem;
      margin-left: 0.1rem;
      margin-right: 0.1rem;
    }
  }
}

</style>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值