当前位置:首页 > JavaScript

js实现倒计时页面跳转

2026-01-30 20:27:41JavaScript

实现倒计时页面跳转的方法

使用JavaScript实现倒计时并在倒计时结束后自动跳转页面,可以通过以下方法完成:

设置倒计时变量和显示

let countdown = 5; // 倒计时5秒
const countdownElement = document.getElementById('countdown'); // 显示倒计时的元素

function updateCountdown() {
  countdownElement.textContent = countdown;
  countdown--;

  if (countdown < 0) {
    clearInterval(timer);
    window.location.href = 'https://example.com'; // 跳转目标URL
  }
}

const timer = setInterval(updateCountdown, 1000); // 每秒更新一次

HTML部分

js实现倒计时页面跳转

<div>
  页面将在 <span id="countdown">5</span> 秒后自动跳转...
</div>

使用setTimeout实现

另一种更简单的方法是使用setTimeout:

setTimeout(() => {
  window.location.href = 'https://example.com';
}, 5000); // 5000毫秒=5秒后跳转

添加进度条效果

如果需要更直观的显示,可以添加进度条:

js实现倒计时页面跳转

const progressBar = document.getElementById('progress');
let width = 100;
const interval = 50; // 更新频率(毫秒)
const duration = 5000; // 总时长(毫秒)
const increment = (interval / duration) * 100;

const progressTimer = setInterval(() => {
  width -= increment;
  progressBar.style.width = width + '%';

  if (width <= 0) {
    clearInterval(progressTimer);
    window.location.href = 'https://example.com';
  }
}, interval);

对应的HTML

<div class="progress-container">
  <div id="progress" class="progress-bar"></div>
</div>

CSS样式

.progress-container {
  width: 100%;
  height: 5px;
  background: #eee;
}

.progress-bar {
  height: 100%;
  background: #4CAF50;
  width: 100%;
  transition: width 0.05s linear;
}

注意事项

  • 确保跳转URL正确无误
  • 考虑添加取消跳转的按钮
  • 在移动设备上测试效果
  • 如果页面内容重要,建议提供明显的手动跳转链接

完整示例代码

<!DOCTYPE html>
<html>
<head>
  <style>
    .progress-container {
      width: 100%;
      height: 5px;
      background: #eee;
      margin: 10px 0;
    }
    .progress-bar {
      height: 100%;
      background: #4CAF50;
      width: 100%;
    }
    #cancel-btn {
      margin-top: 10px;
      padding: 5px 10px;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div>
    页面将在 <span id="countdown">5</span> 秒后自动跳转...
  </div>
  <div class="progress-container">
    <div id="progress" class="progress-bar"></div>
  </div>
  <button id="cancel-btn">取消跳转</button>

  <script>
    let countdown = 5;
    const countdownElement = document.getElementById('countdown');
    const progressBar = document.getElementById('progress');
    const cancelBtn = document.getElementById('cancel-btn');
    let width = 100;
    const interval = 50;
    const duration = 5000;
    const increment = (interval / duration) * 100;

    let progressTimer = setInterval(() => {
      width -= increment;
      progressBar.style.width = width + '%';

      if (width <= 0) {
        clearInterval(progressTimer);
        window.location.href = 'https://example.com';
      }
    }, interval);

    let countdownTimer = setInterval(() => {
      countdown--;
      countdownElement.textContent = countdown;

      if (countdown <= 0) {
        clearInterval(countdownTimer);
      }
    }, 1000);

    cancelBtn.addEventListener('click', () => {
      clearInterval(progressTimer);
      clearInterval(countdownTimer);
      countdownElement.textContent = '已取消';
      progressBar.style.width = '100%';
      progressBar.style.backgroundColor = '#ccc';
    });
  </script>
</body>
</html>

标签: 跳转倒计时
分享给朋友:

相关文章

js怎么实现网页跳转页面跳转

js怎么实现网页跳转页面跳转

JavaScript 实现网页跳转的方法 使用 window.location.href 是最常见的方式,直接修改当前页面的 URL 地址实现跳转。 示例代码: window.location.…

js怎么实现网页跳转页面跳转页面跳转

js怎么实现网页跳转页面跳转页面跳转

JavaScript 实现网页跳转的方法 使用 window.location.href 跳转 通过修改 window.location.href 属性实现页面跳转,这是最常用的方式。例如: w…

vue实现音乐跳转

vue实现音乐跳转

使用 Vue 实现音乐跳转功能 在 Vue 中实现音乐跳转功能,通常需要结合 HTML5 的 <audio> 元素和 Vue 的数据绑定及事件处理机制。以下是实现音乐跳转的几种常见方法。…

vue 实现自动跳转

vue 实现自动跳转

使用 vue-router 实现自动跳转 在 Vue 项目中,可以通过 vue-router 的编程式导航实现页面自动跳转。以下是几种常见场景的实现方法: 基础跳转 // 在组件方法中调用 thi…

vue实现app跳转

vue实现app跳转

Vue 实现 App 跳转的方法 在 Vue 中实现 App 跳转通常涉及以下几种场景:从 H5 页面跳转到原生 App、从 App 内跳转到其他页面或外部链接。以下是具体实现方式: 通过 URL…

vue路由跳转无法实现

vue路由跳转无法实现

路由配置问题 检查router/index.js中的路由配置是否正确。确保已定义目标路由且路径拼写无误。常见错误包括路径缺少斜杠或参数格式错误。 // 错误示例 { path: 'detail'…