当前位置:首页 > JavaScript

用js实现一个倒计时器

2026-03-02 03:37:45JavaScript

实现倒计时器的基本方法

使用JavaScript实现倒计时器可以通过setIntervalrequestAnimationFrame结合时间差计算来完成。以下是两种常见的实现方式。

基于setInterval的倒计时器

创建一个简单的倒计时器,显示剩余时间并支持暂停和重置功能。

class CountdownTimer {
  constructor(duration, displayElement) {
    this.duration = duration; // 总时长(秒)
    this.displayElement = displayElement;
    this.remainingTime = duration;
    this.timerId = null;
  }

  start() {
    if (this.timerId) return;
    this.timerId = setInterval(() => {
      this.remainingTime--;
      this.updateDisplay();
      if (this.remainingTime <= 0) {
        this.stop();
      }
    }, 1000);
  }

  stop() {
    clearInterval(this.timerId);
    this.timerId = null;
  }

  reset() {
    this.stop();
    this.remainingTime = this.duration;
    this.updateDisplay();
  }

  updateDisplay() {
    const minutes = Math.floor(this.remainingTime / 60);
    const seconds = this.remainingTime % 60;
    this.displayElement.textContent = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
  }
}

// 使用示例
const display = document.getElementById('countdown-display');
const timer = new CountdownTimer(300, display); // 5分钟倒计时
document.getElementById('start-btn').addEventListener('click', () => timer.start());
document.getElementById('stop-btn').addEventListener('click', () => timer.stop());
document.getElementById('reset-btn').addEventListener('click', () => timer.reset());

基于requestAnimationFrame的高精度倒计时器

如果需要更高精度或更流畅的动画效果,可以使用requestAnimationFrame

用js实现一个倒计时器

class HighPrecisionCountdown {
  constructor(duration, displayElement) {
    this.duration = duration * 1000; // 转换为毫秒
    this.displayElement = displayElement;
    this.startTime = null;
    this.remainingTime = this.duration;
    this.animationId = null;
  }

  start() {
    if (this.animationId) return;
    this.startTime = performance.now();
    this.animationId = requestAnimationFrame(this.update.bind(this));
  }

  update(timestamp) {
    const elapsed = timestamp - this.startTime;
    this.remainingTime = Math.max(0, this.duration - elapsed);
    this.render();
    if (this.remainingTime > 0) {
      this.animationId = requestAnimationFrame(this.update.bind(this));
    }
  }

  stop() {
    cancelAnimationFrame(this.animationId);
    this.animationId = null;
  }

  reset() {
    this.stop();
    this.remainingTime = this.duration;
    this.render();
  }

  render() {
    const totalSeconds = Math.ceil(this.remainingTime / 1000);
    const minutes = Math.floor(totalSeconds / 60);
    const seconds = totalSeconds % 60;
    this.displayElement.textContent = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
  }
}

// 使用示例
const display = document.getElementById('high-precision-countdown');
const highPrecisionTimer = new HighPrecisionCountdown(300, display);
document.getElementById('start-btn').addEventListener('click', () => highPrecisionTimer.start());
document.getElementById('stop-btn').addEventListener('click', () => highPrecisionTimer.stop());
document.getElementById('reset-btn').addEventListener('click', () => highPrecisionTimer.reset());

注意事项

  • setInterval可能存在时间漂移问题,长时间运行后可能不准确。
  • requestAnimationFrame更适合需要平滑动画的场景,但会消耗更多资源。
  • 页面不可见时(如切换到其他标签页),requestAnimationFrame会暂停执行,而setInterval可能继续运行但会被浏览器限制。

标签: 计时器js
分享给朋友:

相关文章

js实现tab选项卡切换

js实现tab选项卡切换

实现Tab选项卡切换的JavaScript方法 使用纯JavaScript实现Tab切换功能,可以通过监听点击事件动态切换内容显示状态。以下是两种常见实现方式: 基础DOM操作实现 /…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js实现跑马灯

js实现跑马灯

实现跑马灯效果 使用HTML和JavaScript可以轻松实现跑马灯效果。以下是两种常见的实现方式: HTML结构 <div id="marquee"> <span>…

js实现菜单

js实现菜单

实现基本HTML结构 使用HTML创建菜单的基本框架,通常包含<ul>和<li>元素。示例结构如下: <ul id="menu"> <li><…