当前位置:首页 > VUE

vue实现计时器

2026-02-22 18:36:33VUE

实现计时器的基本思路

在Vue中实现计时器功能通常需要利用setIntervalsetTimeout进行时间计算,并通过Vue的响应式特性更新显示。计时器的核心逻辑包括开始、暂停、继续和重置等操作。

基本计时器实现

创建一个简单的计时器组件,包含显示时间和控制按钮:

vue实现计时器

<template>
  <div>
    <h2>{{ formattedTime }}</h2>
    <button @click="startTimer" :disabled="isRunning">开始</button>
    <button @click="pauseTimer" :disabled="!isRunning">暂停</button>
    <button @click="resetTimer">重置</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      time: 0,
      isRunning: false,
      timer: null
    }
  },
  computed: {
    formattedTime() {
      const hours = Math.floor(this.time / 3600)
      const minutes = Math.floor((this.time % 3600) / 60)
      const seconds = this.time % 60
      return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
    }
  },
  methods: {
    startTimer() {
      if (!this.isRunning) {
        this.isRunning = true
        this.timer = setInterval(() => {
          this.time++
        }, 1000)
      }
    },
    pauseTimer() {
      clearInterval(this.timer)
      this.isRunning = false
    },
    resetTimer() {
      clearInterval(this.timer)
      this.isRunning = false
      this.time = 0
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

倒计时实现

倒计时功能与正向计时类似,但需要设置初始时间并递减:

vue实现计时器

<template>
  <div>
    <h2>{{ formattedTime }}</h2>
    <button @click="startCountdown" :disabled="isRunning">开始</button>
    <button @click="pauseCountdown" :disabled="!isRunning">暂停</button>
    <button @click="resetCountdown">重置</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      initialTime: 60, // 初始60秒
      time: 60,
      isRunning: false,
      timer: null
    }
  },
  computed: {
    formattedTime() {
      const minutes = Math.floor(this.time / 60)
      const seconds = this.time % 60
      return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
    }
  },
  methods: {
    startCountdown() {
      if (!this.isRunning) {
        this.isRunning = true
        this.timer = setInterval(() => {
          if (this.time > 0) {
            this.time--
          } else {
            clearInterval(this.timer)
            this.isRunning = false
          }
        }, 1000)
      }
    },
    pauseCountdown() {
      clearInterval(this.timer)
      this.isRunning = false
    },
    resetCountdown() {
      clearInterval(this.timer)
      this.isRunning = false
      this.time = this.initialTime
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

使用Vue 3 Composition API实现

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

<template>
  <div>
    <h2>{{ formattedTime }}</h2>
    <button @click="start" :disabled="isRunning">开始</button>
    <button @click="pause" :disabled="!isRunning">暂停</button>
    <button @click="reset">重置</button>
  </div>
</template>

<script>
import { ref, computed, onBeforeUnmount } from 'vue'

export default {
  setup() {
    const time = ref(0)
    const isRunning = ref(false)
    let timer = null

    const formattedTime = computed(() => {
      const hours = Math.floor(time.value / 3600)
      const minutes = Math.floor((time.value % 3600) / 60)
      const seconds = time.value % 60
      return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
    })

    function start() {
      if (!isRunning.value) {
        isRunning.value = true
        timer = setInterval(() => {
          time.value++
        }, 1000)
      }
    }

    function pause() {
      clearInterval(timer)
      isRunning.value = false
    }

    function reset() {
      clearInterval(timer)
      isRunning.value = false
      time.value = 0
    }

    onBeforeUnmount(() => {
      clearInterval(timer)
    })

    return {
      time,
      isRunning,
      formattedTime,
      start,
      pause,
      reset
    }
  }
}
</script>

性能优化注意事项

避免直接操作DOM,利用Vue的响应式系统更新显示。在组件销毁时务必清除定时器,防止内存泄漏。对于高精度计时需求,可以考虑使用requestAnimationFrame代替setInterval

扩展功能建议

计时器可以扩展添加以下功能:记录多个计时结果(圈数)、自定义初始时间、保存计时记录到本地存储、添加音效提示等。这些功能可以通过添加额外的数据属性和方法来实现。

标签: 计时器vue
分享给朋友:

相关文章

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue实现浮标

vue实现浮标

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

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…