当前位置:首页 > VUE

vue 实现多个倒计

2026-01-19 01:14:08VUE

Vue 实现多个倒计时的方法

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

使用 setIntervalclearInterval

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

<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 实现多个倒计时。

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

动态添加倒计时

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

vue 实现多个倒计

<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 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…

vue弹幕实现

vue弹幕实现

Vue弹幕实现方法 弹幕功能常见于视频直播或评论区,以下是基于Vue的实现方案,涵盖基础弹幕、动画控制及性能优化。 基础弹幕渲染 通过v-for动态渲染弹幕列表,结合CSS实现横向移动效果。…