当前位置:首页 > VUE

vue实现倒计时

2026-02-09 09:00:52VUE

实现倒计时的基本方法

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

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的项目,可以采用更简洁的方式:

vue实现倒计时

<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.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…