当前位置:首页 > 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实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for="…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…

vue怎么实现onclick

vue怎么实现onclick

在Vue中实现点击事件(类似原生onclick)可以通过多种方式完成,以下是常见方法: 使用 v-on 指令 Vue提供了v-on指令绑定DOM事件,简写为@。例如绑定点击事件: <butt…