当前位置:首页 > VUE

vue实现倒计时

2026-01-06 23:05:01VUE

Vue 实现倒计时的基本方法

使用 setInterval 和响应式数据

在 Vue 组件中定义一个响应式变量(如 countdown),通过 setInterval 每秒更新数值。组件销毁时清除定时器避免内存泄漏。

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>

使用 Composition API(Vue 3)

通过 refonMounted/onUnmounted 实现更简洁的逻辑:

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

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

const countdown = ref(60);
let timer = null;

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

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

onUnmounted(() => clearInterval(timer));
</script>

封装可复用的倒计时 Hook(Vue 3)

将逻辑抽离为自定义 Hook,便于多处复用:

vue实现倒计时

// useCountdown.js
import { ref, computed, onUnmounted } from 'vue';

export function useCountdown(initialTime) {
  const countdown = ref(initialTime);
  let timer = null;

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

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

  onUnmounted(() => clearInterval(timer));

  return { countdown, minutes, seconds, start };
}

组件中使用:

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

<script setup>
import { useCountdown } from './useCountdown';

const { minutes, seconds } = useCountdown(60);
</script>

支持暂停和重置功能

扩展基础倒计时,增加控制方法:

<template>
  <div>
    <div>{{ minutes }}:{{ seconds < 10 ? '0' : '' }}{{ seconds }}</div>
    <button @click="pause">暂停</button>
    <button @click="reset">重置</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      countdown: 60,
      timer: null,
      isPaused: false
    }
  },
  methods: {
    pause() {
      this.isPaused = !this.isPaused;
      if (this.isPaused) {
        clearInterval(this.timer);
      } else {
        this.startTimer();
      }
    },
    reset() {
      clearInterval(this.timer);
      this.countdown = 60;
      this.startTimer();
    },
    startTimer() {
      this.timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--;
        } else {
          clearInterval(this.timer);
        }
      }, 1000);
    }
  },
  mounted() {
    this.startTimer();
  }
}
</script>

注意事项

  1. 定时器清理:务必在组件销毁时清除定时器,防止内存泄漏。
  2. 性能优化:频繁操作 DOM 时考虑使用 requestAnimationFrame
  3. 格式化显示:通过计算属性处理时间格式,保持模板简洁。
  4. 跨组件通信:如需全局倒计时,可通过 Vuex/Pinia 管理状态。

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

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue懒加载实现难吗

vue懒加载实现难吗

vue懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Router…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…