当前位置:首页 > VUE

vue实现秒表组件

2026-02-17 05:10:47VUE

实现思路

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

vue实现秒表组件

基础代码结构

<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

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 radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如…

vue实现下拉框组件

vue实现下拉框组件

Vue 下拉框组件实现 基础实现 创建基础下拉框组件,使用v-model实现双向数据绑定: <template> <div class="dropdown"> &…

vue实现组件

vue实现组件

Vue 实现组件的方法 Vue 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用。…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

实现拖拽组件嵌套的核心思路 Vue中实现拖拽组件嵌套需要结合拖拽库(如vuedraggable)和组件递归渲染。核心在于动态管理嵌套数据结构,并通过事件传递实现层级更新。 安装必要依赖 推荐使用vu…