To implement a countdown timer that redirects after five seconds, you can use the following code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业1</title>
</head>

<body>
    <span id="time">5</span>秒之后跳转到官网首页

    <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>
</body>

</html>

This code sets an initial value of 5 for the timeLeft variable. It then uses setInterval to decrement the timeLeft variable and update the content of the <span> element with the id time every second (1000 milliseconds). When timeLeft reaches 0, the clearInterval function is called to stop the countdown timer, and the page is redirected to the desired URL using location.href

!DOCTYPE htmlhtml lang=enhead meta charset=UTF-8 meta http-equiv=X-UA-Compatible content=IE=edge meta name=viewport content=width=device-width initial-scale=10 title作业1titleheadbody spa

原文地址: http://www.cveoy.top/t/topic/iPh7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录