当前位置:首页 > VUE

vue实现倒计时

2026-01-06 23:05:01VUE

Vue 实现倒计时的基本方法

使用 setInterval 和响应式数据

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

<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,便于多处复用:

// 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>

支持暂停和重置功能

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

vue实现倒计时

<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 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue框架实现

vue框架实现

Vue 框架实现方法 Vue 是一个渐进式 JavaScript 框架,适用于构建用户界面。以下是一些核心实现方法,涵盖基本使用、组件化、状态管理和优化技巧。 基本项目搭建 使用 Vue CLI…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…

vue实现拉伸

vue实现拉伸

Vue 实现元素拉伸功能 在Vue中实现元素的拉伸(拖拽调整大小)功能,可以通过监听鼠标事件结合CSS样式来实现。以下是两种常见实现方式: 使用原生事件监听 创建可拉伸的组件需要处理鼠标按下…