当前位置:首页 > JavaScript

js实现秒表

2026-03-14 04:23:10JavaScript

js实现秒表

实现秒表功能

使用JavaScript实现秒表功能可以通过Date对象或performance.now()来精确计时。以下是两种常见的实现方式。

js实现秒表

使用Date对象实现秒表

let startTime;
let elapsedTime = 0;
let timerInterval;

function startStopwatch() {
  startTime = new Date().getTime() - elapsedTime;
  timerInterval = setInterval(updateStopwatch, 10);
}

function stopStopwatch() {
  clearInterval(timerInterval);
}

function resetStopwatch() {
  clearInterval(timerInterval);
  elapsedTime = 0;
  document.getElementById("display").textContent = "00:00:00.000";
}

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

function formatTime(time) {
  const hours = Math.floor(time / (1000 * 60 * 60));
  const minutes = Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
  const seconds = Math.floor((time % (1000 * 60)) / 1000);
  const milliseconds = Math.floor(time % 1000);

  return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}.${pad(milliseconds, 3)}`;
}

function pad(num, size = 2) {
  return num.toString().padStart(size, '0');
}

使用performance.now()实现秒表

performance.now()提供了更高精度的计时,适合需要更精确计时的场景。

let startTime;
let elapsedTime = 0;
let timerInterval;

function startStopwatch() {
  startTime = performance.now() - elapsedTime;
  timerInterval = requestAnimationFrame(updateStopwatch);
}

function stopStopwatch() {
  cancelAnimationFrame(timerInterval);
}

function resetStopwatch() {
  cancelAnimationFrame(timerInterval);
  elapsedTime = 0;
  document.getElementById("display").textContent = "00:00:00.000";
}

function updateStopwatch() {
  const currentTime = performance.now();
  elapsedTime = currentTime - startTime;
  const formattedTime = formatTime(elapsedTime);
  document.getElementById("display").textContent = formattedTime;
  timerInterval = requestAnimationFrame(updateStopwatch);
}

function formatTime(time) {
  const hours = Math.floor(time / (1000 * 60 * 60));
  const minutes = Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
  const seconds = Math.floor((time % (1000 * 60)) / 1000);
  const milliseconds = Math.floor(time % 1000);

  return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}.${pad(milliseconds, 3)}`;
}

function pad(num, size = 2) {
  return num.toString().padStart(size, '0');
}

HTML部分

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

CSS部分(可选)

#display {
  font-family: monospace;
  font-size: 2em;
  margin: 20px;
}

button {
  padding: 10px 20px;
  margin: 5px;
  font-size: 1em;
}

注意事项

  • performance.now()提供微秒级精度,但可能受浏览器限制。
  • setInterval的精度可能受系统负载影响,requestAnimationFrame更适合平滑动画。
  • 使用padStart确保时间格式始终为两位数,保持显示一致。

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

相关文章

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('image…

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…

js防抖和节流实现

js防抖和节流实现

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

js实现dh

js实现dh

JavaScript 实现 DH(Diffie-Hellman)密钥交换 Diffie-Hellman(DH)密钥交换是一种安全协议,允许双方在不安全的通信信道上建立一个共享密钥。以下是如何在 Jav…

js实现路由

js实现路由

js实现路由的方法 在JavaScript中实现路由功能可以通过多种方式完成,以下是几种常见的方法: 使用原生JavaScript实现路由 通过监听window.onhashchange事件来实现基…