当前位置:首页 > 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

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实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-for…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@l…

vue实现横滚

vue实现横滚

Vue 实现横滚效果 横滚效果通常指水平滚动内容,常见于轮播图、横向导航栏等场景。以下是几种实现方式: 使用 CSS 和 Vue 结合 通过 CSS 的 overflow-x 和 white-spa…