定时器

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type ="text/javascript">
        var time = 10;
        var id=0;
        window.onload = function () {
            //时钟每次都会返回一个ID
            id = setInterval(daojishi, 1000);
            setTimeout(daojishi,1000);//当window.onload后,再隔1000ms调用指定的方法
        }
        function daojishi()
        {
            time--;
            document.getElementById("time").innerHTML=time;
            if(time==0)
            {
                clearInterval(id);//清除定时器
            }
        }

    </script>
</head>
<body>
   倒计时 <span  id="time"></span>秒
</body>
</html>

  

posted @ 2014-06-13 23:43  编程猴子  阅读(171)  评论(0编辑  收藏  举报