分:秒:毫秒倒计时

<div class="countDownContent">
      <div class="timeTitle">双12大促限时优惠:</div>
      <div class="countDownBox">
        <div class="timeBox">{{ minutes }}</div>
        :
        <div class="timeBox">{{ seconds }}</div>
        :
        <div class="timeBox">{{ milliseconds }}</div>
      </div>
    </div>
data(){
   return {
     // 倒计时
      totalMilliseconds: 5 * 60 * 1000, // 5分钟转换成毫秒
      timer: null,
      isRunning: false,
   }
}
computed: {
    minutes() {
      const time = this.totalMilliseconds / 1000;
      return Math.floor(time / 60)
        .toString()
        .padStart(2, "0");
    },
    seconds() {
      const time = this.totalMilliseconds / 1000;
      return Math.floor(time % 60)
        .toString()
        .padStart(2, "0");
    },
    milliseconds() {
      return Math.floor((this.totalMilliseconds % 1000) / 10)
        .toString()
        .padStart(2, "0");
    },
  },
methods:{
// 倒计时
    startTimer() {
      if (!this.isRunning) {
        this.isRunning = true;
        this.timer = setInterval(() => {
          if (this.totalMilliseconds > 0) {
            this.totalMilliseconds -= 10; // 每10毫秒减少一次
          } else {
            this.resetTimer();
          }
        }, 10);
      }
    },
    resetTimer() {
      clearInterval(this.timer);
      this.totalMilliseconds = 0; // 倒计时清0
      this.isRunning = false;
      // this.startTimer(); // 重置后立即开始新的计时
    },
}
//生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {
    this.startTimer(); // 页面加载完成后启动计时器
  },
//生命周期 - 销毁之前
  beforeDestroy() {
    clearInterval(this.timer); // 清除计时器,防止内存泄漏
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值