当前位置:首页 > JavaScript

js实现倒计时

2026-02-28 17:24:14JavaScript

实现倒计时的基本方法

使用JavaScript实现倒计时可以通过setIntervalsetTimeout函数来完成。以下是一个简单的倒计时示例:

function countdown(seconds) {
  let timer = setInterval(() => {
    console.log(seconds);
    seconds--;
    if (seconds < 0) {
      clearInterval(timer);
      console.log("倒计时结束");
    }
  }, 1000);
}

countdown(10); // 从10秒开始倒计时

动态更新页面上的倒计时

如果需要在网页上动态显示倒计时,可以通过操作DOM元素来实现:

function updateCountdown(seconds, elementId) {
  const element = document.getElementById(elementId);
  let timer = setInterval(() => {
    element.textContent = seconds;
    seconds--;
    if (seconds < 0) {
      clearInterval(timer);
      element.textContent = "时间到!";
    }
  }, 1000);
}

updateCountdown(10, "countdown-display"); // 更新ID为countdown-display的元素

处理日期倒计时

对于更复杂的倒计时(如距离某个特定日期的时间),可以使用Date对象:

function dateCountdown(targetDate, elementId) {
  const element = document.getElementById(elementId);
  let timer = setInterval(() => {
    const now = new Date();
    const diff = targetDate - now;

    if (diff <= 0) {
      clearInterval(timer);
      element.textContent = "时间到!";
      return;
    }

    const days = Math.floor(diff / (1000 * 60 * 60 * 24));
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((diff % (1000 * 60)) / 1000);

    element.textContent = `${days}天 ${hours}小时 ${minutes}分 ${seconds}秒`;
  }, 1000);
}

const targetDate = new Date("2023-12-31T23:59:59");
dateCountdown(targetDate, "date-countdown");

优化性能

为了避免频繁的DOM操作影响性能,可以使用requestAnimationFrame

function smoothCountdown(seconds, elementId) {
  const element = document.getElementById(elementId);
  let start = Date.now();
  let end = start + seconds * 1000;

  function update() {
    const now = Date.now();
    const remaining = Math.max(0, end - now);
    const secs = Math.ceil(remaining / 1000);

    element.textContent = secs;

    if (remaining > 0) {
      requestAnimationFrame(update);
    } else {
      element.textContent = "时间到!";
    }
  }

  requestAnimationFrame(update);
}

smoothCountdown(10, "smooth-countdown");

添加暂停和继续功能

为了增加交互性,可以实现暂停和继续功能:

js实现倒计时

let timer;
let remainingSeconds = 10;
let isPaused = false;

function toggleCountdown(elementId) {
  const element = document.getElementById(elementId);

  if (isPaused) {
    isPaused = false;
    countdown(remainingSeconds, element);
  } else {
    isPaused = true;
    clearInterval(timer);
  }
}

function countdown(seconds, element) {
  remainingSeconds = seconds;
  timer = setInterval(() => {
    element.textContent = remainingSeconds;
    remainingSeconds--;
    if (remainingSeconds < 0) {
      clearInterval(timer);
      element.textContent = "时间到!";
    }
  }, 1000);
}

countdown(10, "interactive-countdown");

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

相关文章

js实现跳转

js实现跳转

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

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…

js图片上传实现

js图片上传实现

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API实现基础图片上传功能。HTML部分需要设置accept="image/…

js验证码实现

js验证码实现

验证码的基本原理 验证码(CAPTCHA)用于区分人类用户和自动化程序。常见类型包括图形验证码、滑动验证码、短信验证码等。JavaScript 可用于前端验证码的生成和验证逻辑。 图形验证码实现 使…