JavaScript 实现五秒倒计时跳转功能
<script>
let timeLeft = 5;
const countdownTimer = setInterval(() => {
timeLeft--;
document.getElementById('time').textContent = timeLeft;
if (timeLeft === 0) {
clearInterval(countdownTimer);
location.href = 'https://www.google.cn/chrome';
}
}, 1000);
</script>
这段代码首先设置 timeLeft 变量的初始值为 5。然后,它使用 setInterval 函数每秒 (1000 毫秒) 递减 timeLeft 变量,并将 <span> 元素 (id 为 time) 的内容更新为当前的 timeLeft 值。当 timeLeft 达到 0 时,调用 clearInterval 函数停止倒计时,并使用 location.href 将页面重定向到指定的 URL。
原文地址: https://www.cveoy.top/t/topic/qvoJ 著作权归作者所有。请勿转载和采集!