定时函数

来源:互联网 发布:电力人工智能与机器人 编辑:程序博客网 时间:2024/05/22 02:21
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">            <html xmlns="http://www.w3.org/1999/xhtml">            <head>            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />            <title>定时函数</title>            </head>            <body>                <button onclick="testSetTimeout()">3S后跳转到百度</button>                当前时间为:<span id="time"></span><br/>                调用的次数是:<span id="count"></span>                <script>                    function testSetTimeout(){                        //alert("asdf");                        //定时                        setTimeout(function(){                            window.open("http://www.baidu.com");                            },3000);                        }                    var count = 0;                    function showTime(){                        var date = new Date();                        var span = document.getElementById("time");                        var hours = date.getHours();                        var minute = date.getMinutes();                        var second = date.getSeconds();                        span.innerHTML = hours + ":" + minute + ":" +second;                        //调用次数                        document.getElementById("count").innerHTML = count;                        count ++;                        if(count == 11){                            //alert(result);                            //取消定时器                            clearInterval(result);                        }                        //return showTime;                    }                    showTime();                    //设置定时器                    var result = setInterval(showTime,1000);                    //var result2 = setInterval(showTime,1000);                </script>            </body>            </html>