当前位置:首页 > VUE

vue记录时长前端实现

2026-02-09 16:39:45VUE

Vue 前端实现时长记录功能

核心思路
通过 Vue 的响应式数据和生命周期钩子,结合浏览器 API 实现开始、暂停、继续和重置时长的功能。关键点在于利用 setInterval 计时并实时更新显示。

基础实现代码

<template>
  <div>
    <p>已记录时长: {{ formattedTime }}</p>
    <button @click="startTimer">开始</button>
    <button @click="pauseTimer">暂停</button>
    <button @click="resetTimer">重置</button>
  </div>
</template>

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

进阶功能实现

持久化存储
结合 localStorage 实现刷新页面不丢失记录:

mounted() {
  const savedTime = localStorage.getItem('recordedTime')
  if (savedTime) {
    this.seconds = parseInt(savedTime)
  }
},
watch: {
  seconds(newVal) {
    localStorage.setItem('recordedTime', newVal.toString())
  }
}

多段时长记录
记录多个时间段并统计总时长:

data() {
  return {
    timeSegments: [],
    currentStart: null
  }
},
methods: {
  startSegment() {
    this.currentStart = new Date()
  },
  endSegment() {
    if (this.currentStart) {
      const duration = Math.floor((new Date() - this.currentStart) / 1000)
      this.timeSegments.push(duration)
      this.currentStart = null
    }
  },
  totalDuration() {
    return this.timeSegments.reduce((sum, seg) => sum + seg, 0)
  }
}

性能优化方案

使用 requestAnimationFrame
替代 setInterval 实现更精确的计时:

methods: {
  startTimer() {
    if (!this.isRunning) {
      this.isRunning = true
      const startTime = Date.now() - this.seconds * 1000
      const update = () => {
        this.seconds = Math.floor((Date.now() - startTime) / 1000)
        if (this.isRunning) {
          requestAnimationFrame(update)
        }
      }
      requestAnimationFrame(update)
    }
  }
}

Worker 线程计时
避免主线程阻塞:

vue记录时长前端实现

// worker.js
let timer
self.onmessage = function(e) {
  if (e.data === 'start') {
    let seconds = 0
    timer = setInterval(() => {
      self.postMessage(++seconds)
    }, 1000)
  } else if (e.data === 'stop') {
    clearInterval(timer)
  }
}

// Vue组件
created() {
  this.worker = new Worker('./worker.js')
  this.worker.onmessage = (e) => {
    this.seconds = e.data
  }
}

注意事项

  • 页面隐藏时(切换标签页)建议暂停计时,可通过 document.visibilityState 监听
  • 移动端需注意锁屏情况,推荐使用 Background Timer 等插件
  • 长时间运行需考虑数字溢出问题,建议使用 BigInt 或分段记录

以上方案可根据实际需求组合使用,基础实现已能满足多数场景,进阶功能适合需要精确统计或离线保存的场景。

标签: 时长vue
分享给朋友:

相关文章

vue实现aop切面

vue实现aop切面

Vue 实现 AOP 切面编程的方法 在 Vue 中实现 AOP(面向切面编程)可以通过多种方式,以下是几种常见的实现方法: 使用 Vue 混入(Mixins) Vue 的混入机制可以用于实现横切关…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue 实现遮罩

vue 实现遮罩

Vue 实现遮罩层的方法 使用固定定位和透明背景 在Vue中实现遮罩层可以通过CSS固定定位结合透明背景色完成。创建一个全屏遮罩组件,利用position: fixed覆盖整个视窗。 <tem…