vue 倒计时组件

startTime为开始时间戳

endTime为结束时间戳

<template>
    <!-- 倒计时封装:当小于三天的时候出现颜色变为红色 -->
      <div>
          {{text}}<div id="meClocker" :class="{three : timeobj.days <= (sign||3)}">{{clocker}}</div>
      </div> 
</template>


<script>
export default {
  data() {
    return {
      clocker: "", //显示倒计时
      timeobj: null, //时间对象
      text:'下课时间还剩:'
    };
  },
  /**传入时间戳显示倒计时函数【后台时间戳一般都是10位的】
   * @argument start 开始时间(10位时间戳)
   * @argument end   结束时间(10为时间戳)
   * @argument msg   倒计时结束后显示的文本(选填)默认为倒计时已结束
   * @argument sign  倒计时结束前几天特殊显示样式 默认三天后变红
   */
  props: ["startTime", "endTime","sign"],

  created() {
    let timeLag = parseInt(this.endTime) - parseInt(this.startTime);
    let add0 = num => {
      return num < 10 ? "0" + num : num;
    };
    let timeFunction = () => {
      // time为两个时间戳之间相差的秒数
      let time = timeLag--;
      //打印出时间对象
      this.timeobj = {
        seconds: time % 60,
        minutes: Math.floor(time / 60) % 60,
        hours: Math.floor(time / 60 / 60) % 24,
        days: Math.floor(time / 60 / 60 / 24),
        weeks: Math.floor(time / 60 / 60 / 24 / 7),
        months: Math.floor(time / 60 / 60 / 24 / 30),
        years: Math.floor(time / 60 / 60 / 24 / 365)
      };
      this.clocker = 
          `
           ${add0(this.timeobj.hours)}:
           ${add0(this.timeobj.minutes)}: 
           ${add0(this.timeobj.seconds)}
           `;
      // 当时间差小于等于0的时候证明倒计时已经过结束
      if (time <= 0) {
        this.text = '下课啦:'
        clearInterval(go);
      }
    };

    //此处调用函数避免前一秒倒计时不显示
    timeFunction();
    let go = setInterval(function() {
      timeFunction();
    }, 1000);

  }
};

</script>
<style scoped>
.three{
  color: #f00;
  font-size: 20px;
}
</style>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值