当前位置:首页 > VUE

vue实现秒表

2026-01-13 07:40:45VUE

实现秒表功能

在Vue中实现秒表功能需要处理计时逻辑、显示格式以及控制按钮(开始、暂停、重置)。以下是具体实现步骤:

vue实现秒表

创建Vue组件

<template>
  <div class="stopwatch">
    <div class="display">{{ formattedTime }}</div>
    <div class="controls">
      <button @click="start" :disabled="isRunning">Start</button>
      <button @click="pause" :disabled="!isRunning">Pause</button>
      <button @click="reset">Reset</button>
    </div>
  </div>
</template>

定义数据属性

<script>
export default {
  data() {
    return {
      isRunning: false,
      startTime: 0,
      elapsedTime: 0,
      timerInterval: null
    }
  }
}
</script>

实现计时逻辑

methods: {
  start() {
    if (!this.isRunning) {
      this.startTime = Date.now() - this.elapsedTime
      this.timerInterval = setInterval(() => {
        this.elapsedTime = Date.now() - this.startTime
      }, 10)
      this.isRunning = true
    }
  },

  pause() {
    if (this.isRunning) {
      clearInterval(this.timerInterval)
      this.isRunning = false
    }
  },

  reset() {
    clearInterval(this.timerInterval)
    this.isRunning = false
    this.elapsedTime = 0
  }
}

格式化显示时间

computed: {
  formattedTime() {
    const date = new Date(this.elapsedTime)
    const minutes = date.getUTCMinutes().toString().padStart(2, '0')
    const seconds = date.getUTCSeconds().toString().padStart(2, '0')
    const milliseconds = Math.floor(date.getUTCMilliseconds() / 10).toString().padStart(2, '0')
    return `${minutes}:${seconds}.${milliseconds}`
  }
}

添加基本样式

<style scoped>
.stopwatch {
  text-align: center;
  margin: 20px;
}

.display {
  font-size: 3em;
  margin-bottom: 20px;
  font-family: monospace;
}

button {
  margin: 0 5px;
  padding: 8px 16px;
  font-size: 1em;
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>

功能扩展建议

如果需要更精确的计时,可以考虑使用requestAnimationFrame代替setInterval

vue实现秒表

start() {
  if (!this.isRunning) {
    this.startTime = Date.now() - this.elapsedTime
    const update = () => {
      this.elapsedTime = Date.now() - this.startTime
      if (this.isRunning) {
        requestAnimationFrame(update)
      }
    }
    this.isRunning = true
    requestAnimationFrame(update)
  }
}

注意事项

组件销毁时应清除计时器:

beforeDestroy() {
  clearInterval(this.timerInterval)
}

这种实现方式提供了基本的秒表功能,可以根据需要进一步扩展,如添加圈数记录、更精确的时间显示格式等。

标签: 秒表vue
分享给朋友:

相关文章

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…