当前位置:首页 > 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中实现注册功能通常需要结合前端表单和后端API交互。以下是完整的实现方案: 创建注册表单组件 注册表单需要包含用户名、邮箱、密码等基本字段,并进行表单验证: <te…

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…