当前位置:首页 > JavaScript

js 实现秒表

2026-02-02 03:51:02JavaScript

实现秒表的基本思路

使用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()

js 实现秒表

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实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现报表

js实现报表

使用JavaScript实现报表 在JavaScript中实现报表功能可以通过多种方式完成,常见的方法包括使用原生JavaScript、第三方库(如Chart.js、D3.js)或结合后端数据渲染。以…

js实现 功能

js实现 功能

在 JavaScript 中实现功能通常涉及多个关键步骤,具体取决于功能需求。以下是常见功能的实现方法和示例: 基本功能实现 使用函数封装逻辑是最直接的方式。例如,实现一个计算两数之和的函数: f…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…

js 实现递归

js 实现递归

递归的基本概念 递归是指函数直接或间接调用自身的过程。在JavaScript中,递归通常用于解决可以分解为相似子问题的问题,如阶乘、斐波那契数列、树形结构遍历等。 递归的实现要点 基线条件(Base…