定时器setInterval的用法

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>


<h1>还剩余<span id="n">10</span>秒钟跳转</h1>


<script>
    // setTimeout(f,5000);
    // function f() {
    //     location.href = "http://www.baidu.com";
    // }
    var  n = 10;
    var timer = setInterval(f,1000);
    function f(){
        console.log(n);
        if (n>0){
            n--;
            document.getElementById("n").innerText = n;
        }else {
            //清除定时器
            clearInterval(timer);
            //跳转
            location.href = "https://www.cnblogs.com/itdabao";
        }
    }

</script>
</body>
</html>

 

posted @ 2022-03-16 14:32  沫栀  阅读(133)  评论(0)    收藏  举报