当前位置:首页 > JavaScript

js实现秒表

2026-02-01 06:00:51JavaScript

实现秒表功能

使用JavaScript实现秒表功能可以通过Date对象或performance.now()来精确计时,结合setInterval控制更新频率。以下是两种常见实现方式:

基于Date对象的秒表

let startTime;
let timerInterval;
let isRunning = false;

function startStopwatch() {
  if (isRunning) return;
  startTime = new Date().getTime();
  timerInterval = setInterval(updateDisplay, 10);
  isRunning = true;
}

function stopStopwatch() {
  clearInterval(timerInterval);
  isRunning = false;
}

function resetStopwatch() {
  stopStopwatch();
  document.getElementById("display").textContent = "00:00:00.000";
}

function updateDisplay() {
  const currentTime = new Date().getTime();
  const elapsedTime = currentTime - startTime;
  const formattedTime = formatTime(elapsedTime);
  document.getElementById("display").textContent = formattedTime;
}

function 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}`;
}

基于performance.now()的高精度秒表

let startTimestamp;
let accumulatedTime = 0;
let timerInterval;
let isRunning = false;

function startStopwatch() {
  if (isRunning) return;
  startTimestamp = performance.now();
  timerInterval = setInterval(updateDisplay, 10);
  isRunning = true;
}

function stopStopwatch() {
  if (!isRunning) return;
  accumulatedTime += performance.now() - startTimestamp;
  clearInterval(timerInterval);
  isRunning = false;
}

function resetStopwatch() {
  stopStopwatch();
  accumulatedTime = 0;
  document.getElementById("display").textContent = "00:00:00.000";
}

function updateDisplay() {
  const currentTime = isRunning 
    ? accumulatedTime + (performance.now() - startTimestamp)
    : accumulatedTime;
  const formattedTime = formatTime(currentTime);
  document.getElementById("display").textContent = formattedTime;
}

function formatTime(milliseconds) {
  const totalSeconds = Math.floor(milliseconds / 1000);
  const hours = Math.floor(totalSeconds / 3600).toString().padStart(2, '0');
  const minutes = Math.floor((totalSeconds % 3600) / 60).toString().padStart(2, '0');
  const seconds = (totalSeconds % 60).toString().padStart(2, '0');
  const ms = Math.floor(milliseconds % 1000).toString().padStart(3, '0');
  return `${hours}:${minutes}:${seconds}.${ms}`;
}

HTML结构示例

<div id="display">00:00:00.000</div>
<button onclick="startStopwatch()">Start</button>
<button onclick="stopStopwatch()">Stop</button>
<button onclick="resetStopwatch()">Reset</button>

关键点说明

performance.now()Date.now()提供更高精度的时间测量(微秒级),且不受系统时间调整影响。

js实现秒表

定时器间隔设置为10ms(实际精度受浏览器限制),可根据需要调整为更小值(如1ms)或更大值(如100ms)。

js实现秒表

使用padStart()方法确保时间格式始终为两位数/三位数显示,保持界面整齐。

累计时间处理允许暂停后继续计时,而不是每次都从零开始。

两种实现都支持显示小时、分钟、秒和毫秒,可根据需求调整格式函数。

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

相关文章

js实现跳转

js实现跳转

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

js如何实现继承

js如何实现继承

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

js防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(…

使用js实现

使用js实现

使用JavaScript实现 JavaScript可以通过Math.random()函数生成随机数,结合其他方法可以实现多种随机数生成需求。 // 生成0到1之间的随机小数 const rando…

js实现投球

js实现投球

实现投球动画的基本思路 使用JavaScript和CSS动画结合的方式模拟投球效果。核心是通过改变元素的位置、旋转和缩放属性,配合定时器或CSS过渡实现平滑动画。 创建基础HTML结构 <di…