当前位置:首页 > VUE

vue实现倒计时组件

2026-03-06 19:13:20VUE

Vue 倒计时组件实现

功能需求分析

  • 显示剩余时间(天、时、分、秒)
  • 支持自定义时间格式
  • 支持暂停/继续
  • 结束时触发回调事件

基础实现方案

创建 CountDown.vue 组件文件:

vue实现倒计时组件

<template>
  <div class="countdown">
    {{ formattedTime }}
  </div>
</template>

<script>
export default {
  props: {
    targetTime: {
      type: [Date, Number, String],
      required: true
    },
    format: {
      type: String,
      default: 'HH:mm:ss'
    }
  },
  data() {
    return {
      remainingTime: 0,
      timer: null
    }
  },
  computed: {
    formattedTime() {
      const days = Math.floor(this.remainingTime / (1000 * 60 * 60 * 24))
      const hours = Math.floor((this.remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
      const minutes = Math.floor((this.remainingTime % (1000 * 60 * 60)) / (1000 * 60))
      const seconds = Math.floor((this.remainingTime % (1000 * 60)) / 1000)

      return this.format
        .replace('DD', days.toString().padStart(2, '0'))
        .replace('HH', hours.toString().padStart(2, '0'))
        .replace('mm', minutes.toString().padStart(2, '0'))
        .replace('ss', seconds.toString().padStart(2, '0'))
    }
  },
  mounted() {
    this.startCountdown()
  },
  beforeDestroy() {
    this.clearTimer()
  },
  methods: {
    startCountdown() {
      this.clearTimer()
      this.updateRemainingTime()

      this.timer = setInterval(() => {
        this.updateRemainingTime()
      }, 1000)
    },
    updateRemainingTime() {
      const now = new Date().getTime()
      const target = new Date(this.targetTime).getTime()
      this.remainingTime = target - now

      if (this.remainingTime <= 0) {
        this.remainingTime = 0
        this.clearTimer()
        this.$emit('finish')
      }
    },
    clearTimer() {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
    },
    pause() {
      this.clearTimer()
    },
    resume() {
      this.startCountdown()
    }
  }
}
</script>

使用方法

<template>
  <CountDown 
    :target-time="targetDate"
    format="DD天 HH:mm:ss"
    @finish="handleFinish"
  />
</template>

<script>
import CountDown from './CountDown.vue'

export default {
  components: { CountDown },
  data() {
    return {
      targetDate: new Date(Date.now() + 86400000) // 24小时后
    }
  },
  methods: {
    handleFinish() {
      console.log('倒计时结束')
    }
  }
}
</script>

高级功能扩展

vue实现倒计时组件

  1. 毫秒级精度
    
    // 修改定时器间隔
    this.timer = setInterval(() => {
    this.updateRemainingTime()
    }, 100)

// 修改格式显示 const milliseconds = Math.floor(this.remainingTime % 1000) format.replace('SSS', milliseconds.toString().padStart(3, '0'))


2. 自定义渲染
```vue
<template>
  <div class="countdown">
    <slot :time="timeData">
      {{ formattedTime }}
    </slot>
  </div>
</template>

// 添加计算属性
timeData() {
  return {
    days: Math.floor(this.remainingTime / (1000 * 60 * 60 * 24)),
    hours: Math.floor((this.remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
    minutes: Math.floor((this.remainingTime % (1000 * 60 * 60)) / (1000 * 60)),
    seconds: Math.floor((this.remainingTime % (1000 * 60)) / 1000),
    milliseconds: Math.floor(this.remainingTime % 1000),
    total: this.remainingTime
  }
}
  1. 服务端时间同步
    
    async fetchServerTime() {
    const response = await fetch('/api/time')
    const { serverTime } = await response.json()
    this.serverTimeOffset = new Date().getTime() - new Date(serverTime).getTime()
    },

updateRemainingTime() { const now = new Date().getTime() - this.serverTimeOffset // 其余逻辑不变 }



注意事项
- 组件销毁时务必清除定时器
- 处理时区问题时可统一使用UTC时间
- 性能优化:高精度倒计时使用requestAnimationFrame替代setInterval
- 移动端注意页面隐藏时的定时器处理

标签: 倒计时组件
分享给朋友:

相关文章

vue实现组件循环图片

vue实现组件循环图片

Vue 实现组件循环图片的方法 在 Vue 中实现组件循环图片通常可以通过 v-for 指令结合动态数据绑定完成。以下是几种常见的实现方式: 使用静态图片路径数组 假设有一组本地图片路径,可以通过数…

uniapp倒计时

uniapp倒计时

uniapp倒计时的实现方法 在uniapp中实现倒计时功能可以通过多种方式完成,以下是几种常见的实现方法: 使用setInterval实现基础倒计时 通过JavaScript的setInterva…

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

vue实现组件

vue实现组件

Vue 实现组件的方法 Vue 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用。…

vue轮播组件实现

vue轮播组件实现

vue轮播组件实现方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入: <template> <div cl…

uniapp 滑动组件

uniapp 滑动组件

uniapp 滑动组件实现方法 使用swiper组件 uniapp内置的swiper组件可以实现滑动效果,适用于轮播图或页面切换场景。 示例代码: <swiper :indicator-d…