setInterval()和setTimeout()

来源:互联网 发布:管家婆软件创业版 编辑:程序博客网 时间:2024/06/07 15:32

window.setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
window.clearInterval() 方法可取消 setInterval() window.clearInterval()
方法的参数必须是由 setInterval() 返回的 ID 值。

实时显示日期:

var i=0;function abc(){    var mytime=new Date();    var mydiv=document.getElementById('abc');    mydiv.innerText=mytime.toLocaleString();    if(++i==5)    {        window.clearInterval(res);    }}var res=window.setInterval("abc()",1000);

您也可以使用一个按钮来打开定时器和关闭定时器

<!DOCTYPE html><html lang="en"><head>    <meta http-equiv="content" content="text/html;charset=utf-8">    <script type="text/javascript" charset="utf8">        var arr;        function w()        {            arr=window.setInterval("clock()");        }        function clock()        {            var t=new Date();            var wang=document.getElementById("clock");            wang.innerText=t.toLocaleString();        }    </script></head><body>    当前时间是:<span id="clock"></span>    <input onclick="window.clearInterval(arr)" type="button" value="停止计时器">    <input onclick="w()" type="button" value="开始计时器"></body></html>

利用定时器数秒:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8"><script type="text/javascript" charset="utf-8" >    var i=10;    function CountTime()    {        i--;        mybut.value="同意"+i;        if(i==0)        {            //让这个按钮可用            mybut.disabled=false;            window.clearInterval(mytimer);            mybut.value="同意";        }    }    var mytimer=window.setInterval("CountTime()",1000);</script></head><body>    <input type="button" disabled="true" value="同意 10" id="mybut"><br/></body></html>

转载或分享请注明地址:http://blog.csdn.net/w19981220

0 0
原创粉丝点击