JS定时器的使用--数码时钟

<title>无标题文档</title>
<script>
function toDou(n){
    if(n<10){
        return '0'+n;
        }else{
            return ''+n;  //保证返回的都是字符串,避免放在一起成了相加
            }
    }
window.onload=function (){
    var aImg=document.getElementsByTagName('img');
    function tick(){
         var oDate=new Date();
    var str=toDou(oDate.getHours())+toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
    for(var i=0;i<aImg.length;i++){
        aImg[i].src='images/'+str.charAt(i)+'.gif';
        }
        }
    setInterval(tick,1000);
    tick();
    };
</script>
</head>

<body style="background:#999; color:white;">
<img src="images/0.gif">
<img src="images/0.gif">
:
<img src="images/0.gif">
<img src="images/0.gif">
:
<img src="images/0.gif">
<img src="images/0.gif">
</body>

小问题:
var str='abcdef';
alert(str[0]); 在IE7下显示undefined
用str.charAt(0)可以解决这个兼容性问题

数码时钟
获取系统时间
Date对象
getHours、getMinutes、getSeconds
Date对象其他方法
年 getFullYear()
月 getMonth() 从0开始
日 getDate()
星期 getDay() 0,1,2,3,4,5,6 0代表周末

原文地址:https://www.cnblogs.com/919czzl/p/4314074.html