当前位置:首页 > VUE

vue实现秒表

2026-01-13 07:40:45VUE

实现秒表功能

在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 实现目录功能 在 Vue 中实现目录功能通常需要动态生成目录结构,并支持点击跳转到对应内容区域。以下是几种常见实现方式: 基于滚动监听和动态生成 安装依赖(如需): npm install…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue表格重置怎么实现

vue表格重置怎么实现

重置表格数据的方法 在Vue中重置表格数据通常需要操作绑定的数据源,以下是几种常见实现方式: 直接重置数据源 // 假设表格绑定的是tableData this.tableData = [..…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…