当前位置:首页 > VUE

vue 实现多个倒计

2026-02-19 17:38:18VUE

实现多个独立倒计时

在Vue中实现多个独立倒计时可以通过v-for循环渲染多个计时器组件,每个组件管理自己的倒计时状态。以下是实现方式:

<template>
  <div>
    <div v-for="(timer, index) in timers" :key="index">
      <p>倒计时 {{ index + 1 }}: {{ formatTime(timer.remaining) }}</p>
      <button @click="startTimer(index)">开始</button>
      <button @click="pauseTimer(index)">暂停</button>
      <button @click="resetTimer(index)">重置</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timers: [
        { remaining: 60, interval: null, isRunning: false },
        { remaining: 120, interval: null, isRunning: false },
        { remaining: 180, interval: null, isRunning: false }
      ]
    }
  },
  methods: {
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60)
      const secs = seconds % 60
      return `${mins}:${secs < 10 ? '0' : ''}${secs}`
    },
    startTimer(index) {
      if (this.timers[index].isRunning) return

      this.timers[index].isRunning = true
      this.timers[index].interval = setInterval(() => {
        if (this.timers[index].remaining > 0) {
          this.timers[index].remaining--
        } else {
          clearInterval(this.timers[index].interval)
          this.timers[index].isRunning = false
        }
      }, 1000)
    },
    pauseTimer(index) {
      clearInterval(this.timers[index].interval)
      this.timers[index].isRunning = false
    },
    resetTimer(index) {
      this.pauseTimer(index)
      this.timers[index].remaining = [60, 120, 180][index]
    }
  },
  beforeDestroy() {
    this.timers.forEach(timer => {
      clearInterval(timer.interval)
    })
  }
}
</script>

使用计算属性优化显示

添加计算属性可以更好地管理时间格式显示:

computed: {
  formattedTimers() {
    return this.timers.map(timer => {
      return {
        ...timer,
        display: this.formatTime(timer.remaining)
      }
    })
  }
}

使用Vuex管理全局状态

当需要跨组件共享倒计时状态时,可以使用Vuex:

// store.js
export default new Vuex.Store({
  state: {
    timers: []
  },
  mutations: {
    UPDATE_TIMER(state, { index, remaining }) {
      state.timers[index].remaining = remaining
    },
    SET_TIMER_INTERVAL(state, { index, interval }) {
      state.timers[index].interval = interval
    }
  },
  actions: {
    startTimer({ commit, state }, index) {
      const interval = setInterval(() => {
        if (state.timers[index].remaining > 0) {
          commit('UPDATE_TIMER', {
            index,
            remaining: state.timers[index].remaining - 1
          })
        } else {
          clearInterval(state.timers[index].interval)
        }
      }, 1000)
      commit('SET_TIMER_INTERVAL', { index, interval })
    }
  }
})

使用Composition API实现

在Vue 3中可以使用Composition API更简洁地实现:

import { ref, onUnmounted } from 'vue'

export default {
  setup() {
    const timers = ref([
      { remaining: 60, interval: null },
      { remaining: 120, interval: null },
      { remaining: 180, interval: null }
    ])

    const formatTime = (seconds) => {
      const mins = Math.floor(seconds / 60)
      const secs = seconds % 60
      return `${mins}:${secs < 10 ? '0' : ''}${secs}`
    }

    const startTimer = (index) => {
      if (timers.value[index].interval) return

      timers.value[index].interval = setInterval(() => {
        if (timers.value[index].remaining > 0) {
          timers.value[index].remaining--
        } else {
          clearInterval(timers.value[index].interval)
          timers.value[index].interval = null
        }
      }, 1000)
    }

    onUnmounted(() => {
      timers.value.forEach(timer => {
        if (timer.interval) clearInterval(timer.interval)
      })
    })

    return { timers, formatTime, startTimer }
  }
}

注意事项

清除定时器非常重要,避免组件销毁后定时器继续运行导致内存泄漏。在beforeDestroyonUnmounted钩子中清除所有定时器。

vue 实现多个倒计

对于复杂场景,可以考虑使用第三方库如countdown.jsday.js来处理时间格式和计算。

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

相关文章

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue  select实现

vue select实现

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

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈(如是否使用 Vue Router)。 使用 Vue Router 进行编程式导航…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…