当前位置:首页 > 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 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue 实现菜单

vue 实现菜单

Vue 实现菜单的方法 使用 Vue 实现菜单可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 实现动态路由菜单 通过 Vue Router 可以动态生成菜单,根据路由配置自动渲…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现gridlayout

vue实现gridlayout

Vue 实现 Grid Layout 使用 CSS Grid 布局 CSS Grid 是一种强大的布局系统,可以直接在 Vue 组件的样式中使用。通过定义网格容器和网格项,可以快速实现复杂的布局结构。…