HTML 定时跳转网页

1:定时器
····
<html>
<head>
<script>
/*
setTimeout 设置过期时间
setTimeout(时间到了之后要执行的行为,什么时间 毫秒 开始执行)
setInterval 设置中断时间
setInterval(时间到了之后要执行的行为 , 间隔多长时间再执行 )
*/

        // 两秒之后执行,执行完当前函数,不再重复执行
       setTimeout(function(){
            console.log("爆炸了");
       } , 2000);
       // 只是中断,每个一段时间休息一回,然后接着执行
       setInterval(function(){
            console.log("死了一回");
       } , 2000 );

       function daojishi(){
           let t = new Date();
           document.getElementById("showTime").innerHTML = t.toLocaleTimeString();
       }

       let fafuqushi = setInterval( daojishi , 1000 );


       function stop(){
           clearInterval(fafuqushi);
       }

    </script>

</head>


<body>

    <div id="showTime">

    </div>

    <button onclick="stop()">你敢按吗?</button>
    

</body>

</html>
····
2:一闪一闪亮晶晶
····
html>
<head>
<style>
#container{
width: 400px;
height: 400px;
border: 1px solid yellowgreen;
background-color: black;
position: relative;
}

        span{
            font-size: 30px;
            color: yellow;
            position: absolute;
        }

    </style>
    <script>
        var timer;
        function start_flash(){
            timer = setInterval(function(){
                document.getElementById("container").innerHTML = "<span> * </span> ";

               let x  = parseInt( Math.random()*400 + 1 );
               let y  = parseInt( Math.random()*400 + 1 );

                document.getElementById("container").firstElementChild.style.left  = x + "px";
                document.getElementById("container").firstElementChild.style.top  = y + "px";

            } , 200 );

        }

        function stop_flash(){
            clearInterval(timer);
        }

    </script>

</head>
<body>
    <div id="container">
    </div>
    <button id="star_flash" onclick="start_flash()"> 亮晶晶 </button>
    <button id="star_flash" onclick="stop_flash()"> 停止亮晶晶 </button>

    

</body>

</html>
····
3:定时跳转网页
····
<html>
<head>
<style>
#box{
width: 1300px;
height: 100px;
line-height: 100px;
border: 1px solid black;
color: yellowgreen;
font: 29/30px "simsun";
text-align: center;
}
</style>

</head>

<body>
    <div id="box">
        5 秒之后,跳转到百度
    </div>

</body>

<script>
    var div = document.getElementById("box");
    var num = 5;

    var timer = null;
    timer = setInterval(() => {
        num --;
        div.innerHTML = num +"秒之后跳转百度";

        if (num == 0) {
            clearInterval(timer);
            location.href = "http://www.baidu.com";
        }
    }, 1000 );
</script>

</html>
····
4:弹窗
····
<html>
<head>
<script>
alert("我是弹窗");
window.alert("全写的窗口弹窗");

        confirm("消息确认窗口");


        let age = prompt("请输入你的年龄");
        alert(age);
        
    </script>


</head>


<body>
    


</body>

</html>
····

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容