设置规定时间后进行跳转

Posted on 2022-03-16 13:59  君子不卿  阅读(46)  评论(0)    收藏  举报
<body>
<h1>页面将在 <span style="font-size: 30px;color: red"> <span id="n">5 </span></span>秒钟后跳转</h1>
<script>
    var timer = setInterval(show,1000);
    var n = 5;
    function show(){
        // console.log(Math.random());
        if(n>0){
            n--;
            document.getElementById("n").innerText = n;
        }else{
            clearInterval(timer);
            location.href = "http://www.baidu.com";
        }
        // console.log(Math.random());
    }
    // setTimeout(f,5000);
    // function f() {
    //     location.href = "http://www.baidu.com";
    // }
</script>
</body>

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3

//评论