当前位置:首页 > JavaScript

js 实现秒表

2026-02-02 03:51:02JavaScript

js 实现秒表

实现秒表的基本思路

使用JavaScript实现秒表需要利用Date对象或performance.now()来精确计时,通过setIntervalrequestAnimationFrame更新显示时间。核心功能包括开始、暂停、重置和显示时间。

基本HTML结构

<div id="stopwatch">
  <div id="display">00:00:00.000</div>
  <button id="startBtn">开始</button>
  <button id="pauseBtn">暂停</button>
  <button id="resetBtn">重置</button>
</div>

JavaScript实现代码

class Stopwatch {
  constructor(displayElement) {
    this.display = displayElement;
    this.isRunning = false;
    this.startTime = 0;
    this.elapsedTime = 0;
    this.timer = null;
  }

  start() {
    if (!this.isRunning) {
      this.isRunning = true;
      this.startTime = Date.now() - this.elapsedTime;
      this.timer = setInterval(() => this.update(), 10);
    }
  }

  pause() {
    if (this.isRunning) {
      this.isRunning = false;
      clearInterval(this.timer);
    }
  }

  reset() {
    this.isRunning = false;
    clearInterval(this.timer);
    this.elapsedTime = 0;
    this.update();
  }

  update() {
    const currentTime = Date.now();
    this.elapsedTime = currentTime - this.startTime;
    this.display.textContent = this.formatTime(this.elapsedTime);
  }

  formatTime(milliseconds) {
    const date = new Date(milliseconds);
    const hours = date.getUTCHours().toString().padStart(2, '0');
    const minutes = date.getUTCMinutes().toString().padStart(2, '0');
    const seconds = date.getUTCSeconds().toString().padStart(2, '0');
    const ms = date.getUTCMilliseconds().toString().padStart(3, '0');
    return `${hours}:${minutes}:${seconds}.${ms}`;
  }
}

// 初始化秒表
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());

高精度计时方案

对于需要更高精度的场景,可以使用performance.now()替代Date.now()

start() {
  if (!this.isRunning) {
    this.isRunning = true;
    this.startTime = performance.now() - this.elapsedTime;
    this.timer = requestAnimationFrame(() => this.update());
  }
}

update() {
  if (!this.isRunning) return;
  const currentTime = performance.now();
  this.elapsedTime = currentTime - this.startTime;
  this.display.textContent = this.formatTime(this.elapsedTime);
  this.timer = requestAnimationFrame(() => this.update());
}

pause() {
  if (this.isRunning) {
    this.isRunning = false;
    cancelAnimationFrame(this.timer);
  }
}

功能扩展建议

  • 添加分段计时(Lap)功能
  • 支持保存历史记录
  • 添加主题切换或自定义样式
  • 实现响应式设计适配移动设备

注意事项

  • setInterval在非活动标签页中可能会降低精度,requestAnimationFrame更适用于动画场景
  • 清除定时器时务必使用clearIntervalcancelAnimationFrame
  • 时间格式化时注意时区问题(使用getUTC方法避免时区影响)

js 实现秒表

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

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 //…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 使用JavaScript实现拖拽功能需要监听鼠标事件,包括mousedown、mousemove和mouseup。以下是实现的基本逻辑: const draggableEleme…

js实现继承

js实现继承

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

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…