欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在几秒之后跳转页面

程序员文章站 2022-03-03 08:53:29
...
<!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>Document</title>
</head>

<body>
    <p></p>
    <button>按钮</button>
    <script>
        var btn = document.querySelector("button");
        var p = document.querySelector("p");
        btn.addEventListener('click', function () {
            location.href = "www.baidu.com";
        })
        var time = 5;
        var timer = setInterval(function () {
            if (time == 0) {
                location.href = "www.baidu.com";
                // clearInterval(timer);
                // time = 5;

            } else {

                p.innerHTML = "您将在" + time + "秒后跳回去";
                time--;
            }
        }, 1000)

    </script>
</body>

</html>
相关标签: js