当前位置:首页 > JavaScript

js实现秒表

2026-04-05 22:28:40JavaScript

实现秒表的基本思路

使用JavaScript实现秒表需要记录开始时间、暂停时间和总运行时间。通过Date对象或performance.now()获取高精度时间戳,结合setInterval更新显示。

核心代码结构

class Stopwatch {
  constructor(displayElement) {
    this.display = displayElement;
    this.running = false;
    this.startTime = 0;
    this.pausedTime = 0;
    this.totalElapsed = 0;
  }

  start() {
    if (!this.running) {
      this.startTime = Date.now() - this.totalElapsed;
      this.running = true;
      this.updateDisplay();
    }
  }

  pause() {
    if (this.running) {
      this.totalElapsed = Date.now() - this.startTime;
      this.running = false;
    }
  }

  reset() {
    this.running = false;
    this.totalElapsed = 0;
    this.updateDisplay();
  }

  updateDisplay() {
    if (this.running) {
      const elapsed = Date.now() - this.startTime;
      this.display.textContent = this.formatTime(elapsed);
      requestAnimationFrame(() => this.updateDisplay());
    } else {
      this.display.textContent = this.formatTime(this.totalElapsed);
    }
  }

  formatTime(ms) {
    const date = new Date(ms);
    return date.toISOString().substr(11, 8);
  }
}

HTML部分

<div id="display">00:00:00</div>
<button id="startBtn">Start</button>
<button id="pauseBtn">Pause</button>
<button id="resetBtn">Reset</button>

初始化与事件绑定

const display = document.getElementById('display');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const resetBtn = document.getElementById('resetBtn');

const stopwatch = new Stopwatch(display);

startBtn.addEventListener('click', () => stopwatch.start());
pauseBtn.addEventListener('click', () => stopwatch.pause());
resetBtn.addEventListener('click', () => stopwatch.reset());

性能优化方案

使用requestAnimationFrame替代setInterval实现更流畅的动画效果。对于高精度需求,可用performance.now()替代Date.now(),其精度达到微秒级。

// 修改start方法中的时间获取方式
this.startTime = performance.now() - this.totalElapsed;

// 修改updateDisplay中的计算方式
const elapsed = performance.now() - this.startTime;

扩展功能

添加分段计时功能:

js实现秒表

class Stopwatch {
  // ...原有代码...
  addLap() {
    if (this.running) {
      const lapTime = Date.now() - this.startTime;
      console.log(`Lap: ${this.formatTime(lapTime)}`);
    }
  }
}

// 添加按钮事件
document.getElementById('lapBtn').addEventListener('click', () => stopwatch.addLap());

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

相关文章

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js如何实现继承

js如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现验证

js实现验证

验证表单输入 使用JavaScript验证表单输入是常见的需求。可以通过监听表单提交事件,检查输入字段是否符合要求。 document.getElementById('myForm').addEve…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…