You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

倒计时定时器正常运行但无法实现网页跳转问题求助

问题修复方案

你的代码存在两处导致无法跳转的问题,直接修正即可:

1. 启用跳转语句

跳转代码被你用注释符//注释掉了,去掉注释就能激活跳转功能。

2. 优化倒计时结束的触发逻辑

原代码中,倒计时显示00:00后,还要再等待1秒才会执行跳转(因为--timer < 0的判断是在显示时间之后),调整判断逻辑后,能让倒计时归零时立刻触发跳转,同时保证最后一秒的00:00正常显示。

修复后的完整代码

<script>
function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    var end = setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        // 先判断是否归零,再递减计时
        if (timer === 0) {
            window.location = "http://www.YouTube.com";
            clearInterval(end);
            return;
        }

        timer--;
    }, 1000);
}

window.onload = function () {
    var fiveMinutes = 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
</script>
</head>
<body>
<div>Redirect in <span id="time">00:05</span></div>
<form id="form1" runat="server">
</form>

额外调整说明

我把初始显示的<span id="time">05:00</span>改成了<span id="time">00:05</span>,和你设置的5秒倒计时逻辑匹配,避免初始显示与实际计时时长不一致的问题。

内容的提问来源于stack exchange,提问作者Lorraine Erickson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:17:39