JS中window对象的使用

本文通过实例演示了如何使用JavaScript中的window对象进行警告、确认对话框及输入提示的操作,并介绍了如何利用setTimeout和setInterval实现定时和间隔执行的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>window对象的使用</title>
        <!--
            作者:李瑞琦
            时间:2017-08-23
            描述:
        -->
        <script type="text/javascript">
            function testAlert(){
                window.alert("我是警告框");
            }
            function testConfirm(){
                var flag = window.confirm("你确定要退出吗");
            }
            function testPrompt(){
                var flag = window.prompt("请输入昵称");
            }
            //定时执行
            //定义全局变量记录线程id
            var id;
            var num;
            //定时执行
            function testTimeOut(){
                id = window.setTimeout(function(){
                    alert("定时执行");
                },2000);
            }
            //结束定时执行的线程
            function clearTimeOut(){
                window.clearTimeout(id);
            }
            //间隔执行
            function testInterval(){
                num = window.setInterval(function(){
                    alert("间隔执行")
                },3000);
            }
            //结束间隔执行的线程
            function clearInterval(){
                window.clearInterval(num);
            }
        </script>
    </head>
    <body>
        <h3>window对象的使用</h3>
        <hr />
        <input type="button" id="" name="" value="testAlert" onclick="testAlert()" />
        <input type="button" id="" name="" value="testConfirm" onclick="testConfirm()" />
        <input type="button" id="" name="" value="testPrompt" onclick="testPrompt()" />
        <hr />
        <input type="button" id="" name="" value="testTimeout" onclick="testTimeOut()" />
        <input type="button" id="" name="" value="testClearTimeout" onclick="clearTimeOut()" />
        <input type="button" id="" name="" value="testInterval" onclick="testInterval()" />
        <input type="button" id="" name="" value="clearInterval" onclick="clearInterval()" />
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值