当前位置:首页 > VUE

vue实现倒计时

2026-02-09 09:00:52VUE

实现倒计时的基本方法

在Vue中实现倒计时功能,可以通过计算剩余时间并动态更新显示。以下是一个简单的实现方式:

<template>
  <div>{{ formattedTime }}</div>
</template>

<script>
export default {
  data() {
    return {
      countdown: 60, // 初始倒计时秒数
      timer: null
    }
  },
  computed: {
    formattedTime() {
      const minutes = Math.floor(this.countdown / 60)
      const seconds = this.countdown % 60
      return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`
    }
  },
  mounted() {
    this.timer = setInterval(() => {
      if (this.countdown > 0) {
        this.countdown--
      } else {
        clearInterval(this.timer)
      }
    }, 1000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

使用Vue Composition API实现

对于Vue 3或使用Composition API的项目,可以采用更简洁的方式:

<template>
  <div>{{ minutes }}:{{ seconds < 10 ? '0' : '' }}{{ seconds }}</div>
</template>

<script>
import { ref, computed, onMounted, onUnmounted } from 'vue'

export default {
  setup() {
    const countdown = ref(60)
    const timer = ref(null)

    const minutes = computed(() => Math.floor(countdown.value / 60))
    const seconds = computed(() => countdown.value % 60)

    onMounted(() => {
      timer.value = setInterval(() => {
        if (countdown.value > 0) {
          countdown.value--
        } else {
          clearInterval(timer.value)
        }
      }, 1000)
    })

    onUnmounted(() => {
      clearInterval(timer.value)
    })

    return { minutes, seconds }
  }
}
</script>

带回调函数的倒计时组件

可以创建一个可复用的倒计时组件,支持完成回调:

<template>
  <div>{{ timeText }}</div>
</template>

<script>
export default {
  props: {
    duration: {
      type: Number,
      default: 60
    },
    onFinish: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      remaining: this.duration
    }
  },
  computed: {
    timeText() {
      const mins = Math.floor(this.remaining / 60)
      const secs = this.remaining % 60
      return `${mins}:${secs < 10 ? '0' : ''}${secs}`
    }
  },
  mounted() {
    this.startCountdown()
  },
  methods: {
    startCountdown() {
      this.timer = setInterval(() => {
        this.remaining--
        if (this.remaining <= 0) {
          clearInterval(this.timer)
          this.onFinish()
        }
      }, 1000)
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

使用第三方库

对于更复杂的倒计时需求,可以考虑使用专门的库如vue-countdown

<template>
  <countdown :time="time" :interval="100" tag="p">
    <template slot-scope="props">{{ props.minutes }}:{{ props.seconds }}.{{ props.milliseconds }}</template>
  </countdown>
</template>

<script>
import Countdown from 'vue-countdown'

export default {
  components: { Countdown },
  data() {
    return {
      time: 60 * 1000 // 60秒
    }
  }
}
</script>

注意事项

实现倒计时功能时需要注意内存管理,确保在组件销毁时清除定时器。倒计时精度取决于JavaScript事件循环,对于高精度需求可能需要特殊处理。跨标签页时,浏览器可能会降低定时器精度以节省资源。

vue实现倒计时

标签: 倒计时vue
分享给朋友:

相关文章

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…