当前位置:首页 > VUE

vue 实现多个倒计

2026-01-19 01:14:08VUE

Vue 实现多个倒计时的方法

在 Vue 中实现多个倒计时可以通过以下方式完成:

使用 setIntervalclearInterval

通过 Vue 的 dataref(Composition API)存储倒计时数据,并使用 setIntervalclearInterval 控制计时逻辑。

vue 实现多个倒计

<template>
  <div v-for="(timer, index) in timers" :key="index">
    倒计时 {{ index + 1 }}: {{ timer.remainingTime }} 秒
  </div>
</template>

<script>
export default {
  data() {
    return {
      timers: [
        { remainingTime: 60, intervalId: null },
        { remainingTime: 120, intervalId: null },
      ],
    };
  },
  mounted() {
    this.timers.forEach((timer, index) => {
      timer.intervalId = setInterval(() => {
        if (timer.remainingTime > 0) {
          timer.remainingTime--;
        } else {
          clearInterval(timer.intervalId);
        }
      }, 1000);
    });
  },
  beforeDestroy() {
    this.timers.forEach((timer) => {
      if (timer.intervalId) clearInterval(timer.intervalId);
    });
  },
};
</script>

使用 Vue 3 Composition API

在 Vue 3 中,可以使用 refonMounted 实现多个倒计时。

vue 实现多个倒计

<template>
  <div v-for="(timer, index) in timers" :key="index">
    倒计时 {{ index + 1 }}: {{ timer.remainingTime }} 秒
  </div>
</template>

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

const timers = ref([
  { remainingTime: 60, intervalId: null },
  { remainingTime: 120, intervalId: null },
]);

onMounted(() => {
  timers.value.forEach((timer) => {
    timer.intervalId = setInterval(() => {
      if (timer.remainingTime > 0) {
        timer.remainingTime--;
      } else {
        clearInterval(timer.intervalId);
      }
    }, 1000);
  });
});

onBeforeUnmount(() => {
  timers.value.forEach((timer) => {
    if (timer.intervalId) clearInterval(timer.intervalId);
  });
});
</script>

使用第三方库(如 vue-countdown

如果需要更复杂的功能,可以使用第三方库 vue-countdown 实现多个倒计时。

<template>
  <div v-for="(time, index) in countdownTimes" :key="index">
    <countdown :time="time" v-slot="{ minutes, seconds }">
      倒计时 {{ index + 1 }}: {{ minutes }}:{{ seconds }}
    </countdown>
  </div>
</template>

<script>
import Countdown from 'vue-countdown';

export default {
  components: { Countdown },
  data() {
    return {
      countdownTimes: [60000, 120000], // 毫秒为单位
    };
  },
};
</script>

动态添加倒计时

如果需要动态添加倒计时,可以通过方法控制倒计时的增减。

<template>
  <div v-for="(timer, index) in timers" :key="index">
    倒计时 {{ index + 1 }}: {{ timer.remainingTime }} 秒
    <button @click="removeTimer(index)">移除</button>
  </div>
  <button @click="addTimer">添加倒计时</button>
</template>

<script>
export default {
  data() {
    return {
      timers: [],
    };
  },
  methods: {
    addTimer() {
      const newTimer = {
        remainingTime: 30,
        intervalId: null,
      };
      newTimer.intervalId = setInterval(() => {
        if (newTimer.remainingTime > 0) {
          newTimer.remainingTime--;
        } else {
          clearInterval(newTimer.intervalId);
        }
      }, 1000);
      this.timers.push(newTimer);
    },
    removeTimer(index) {
      clearInterval(this.timers[index].intervalId);
      this.timers.splice(index, 1);
    },
  },
  beforeDestroy() {
    this.timers.forEach((timer) => {
      if (timer.intervalId) clearInterval(timer.intervalId);
    });
  },
};
</script>

注意事项

  • 使用 setInterval 时,确保在组件销毁时清除定时器以避免内存泄漏。
  • 如果需要更精确的倒计时,可以使用 requestAnimationFrameperformance.now() 替代 setInterval
  • 在 Vue 3 中,推荐使用 Composition API 结合 refonMounted 管理倒计时逻辑。

标签: 多个vue
分享给朋友:

相关文章

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template&…

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vu…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用X…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…