当前位置:首页 > VUE

vue实现秒表组件

2026-02-17 05:10:47VUE

实现思路

使用Vue的响应式特性,通过data属性记录当前时间、计时状态及间隔ID。核心逻辑包括开始、暂停、继续和重置功能,利用setInterval实现计时更新。

基础代码结构

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

<script>
export default {
  data() {
    return {
      startTime: 0,
      elapsedTime: 0,
      isRunning: false,
      intervalId: null,
      hasStarted: false
    };
  },
  computed: {
    formattedTime() {
      const ms = this.elapsedTime % 1000;
      const seconds = Math.floor(this.elapsedTime / 1000) % 60;
      const minutes = Math.floor(this.elapsedTime / 60000) % 60;
      const hours = Math.floor(this.elapsedTime / 3600000);

      return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${ms.toString().padStart(3, '0')}`;
    }
  },
  methods: {
    start() {
      this.startTime = Date.now() - this.elapsedTime;
      this.isRunning = true;
      this.hasStarted = true;
      this.intervalId = setInterval(this.updateTime, 10);
    },
    pause() {
      clearInterval(this.intervalId);
      this.isRunning = false;
    },
    reset() {
      clearInterval(this.intervalId);
      this.elapsedTime = 0;
      this.isRunning = false;
      this.hasStarted = false;
    },
    updateTime() {
      this.elapsedTime = Date.now() - this.startTime;
    }
  },
  beforeDestroy() {
    clearInterval(this.intervalId);
  }
};
</script>

<style scoped>
.stopwatch {
  text-align: center;
  font-family: monospace;
}
.display {
  font-size: 2em;
  margin-bottom: 10px;
}
button {
  margin: 0 5px;
  padding: 5px 10px;
}
</style>

功能扩展

添加分段计时功能
data中新增laps数组,并添加记录分段的方法:

data() {
  return {
    laps: []
  };
},
methods: {
  recordLap() {
    this.laps.push(this.formattedTime);
  }
}

优化性能
使用requestAnimationFrame替代setInterval

vue实现秒表组件

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

注意事项

  1. 清除定时器:在组件销毁前通过beforeDestroy钩子清除定时器,避免内存泄漏。
  2. 时间精度:根据需求调整setInterval的间隔(如10ms),更高精度需使用performance.now()
  3. 状态管理:通过isRunninghasStarted控制按钮的禁用状态,避免逻辑冲突。

标签: 秒表组件
分享给朋友:

相关文章

vue实现秒表

vue实现秒表

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

vue实现树组件

vue实现树组件

Vue 树形组件的实现方法 递归组件实现 使用 Vue 的递归组件特性可以轻松实现树形结构。定义一个组件,该组件能够调用自身来渲染子节点。 <template> <ul>…

vue实现搜索组件

vue实现搜索组件

Vue 实现搜索组件的方法 基础搜索组件实现 创建基础搜索组件需要包含输入框和搜索按钮。在Vue中可以使用v-model绑定输入值,通过@click或@keyup.enter触发搜索。 <te…

vue实现附件组件

vue实现附件组件

Vue 实现附件组件的方法 基础组件结构 使用 Vue 的单文件组件(SFC)方式创建一个基础的附件上传组件。需要包含文件选择、预览、上传和删除功能。 <template> <…

vue实现拖拽组件

vue实现拖拽组件

Vue 实现拖拽组件的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现动态组件

vue实现动态组件

动态组件的基本概念 在Vue中,动态组件允许根据条件或用户交互切换不同的组件,无需重复编写模板逻辑。核心是通过<component>标签的is属性绑定组件名或组件对象。 使用is属性切换…