• 各种计时


    1.文字时间

      

    window.onload=function(){
                    function todou(n){  //封装的补零函数
                        if(n<10){return '0'+n;}
                        else{return ''+n;}
                    }
                    function tick(){
                        var oDiv=document.getElementById("box");
                        var oDate=new Date();
                        var year=oDate.getFullYear();
                        var month=oDate.getMonth();  //0-11
                        var day=oDate.getDate();
                        var week=oDate.getDay();  //0-6,0代表星期日
                        var hours=oDate.getHours();
                        var minutes=oDate.getMinutes();
                        var seconds=oDate.getSeconds();
                      if(week==2)week='星期二';
                      oDiv.innerHTML=year+'年'+(month+1)+'月'+day+'日'+week+todou(hours)+':'+todou(minutes)+':'+todou(seconds);
                      }
                    tick();
                    setInterval(tick,1000);
                }

    2.图片时间

        

    window.onload=function(){
                    var aImg=document.getElementsByTagName('img');
                    function toDou(n){
                        if(n<10){return '0'+n;}
                        else{return ''+n;}
                    }
                    function tick(){
                        var oDate=new Date();
                        var H=oDate.getHours();
                        var M=oDate.getMinutes();
                        var S=oDate.getSeconds();
                        str=toDou(H)+toDou(M)+toDou(S);
                        for(var i=0;i<aImg.length;i++){
                        aImg[i].src='img/'+str.charAt(i)+'.png';
                      }
                    }
                    tick();
                    setInterval(tick,1000);
                }

    3.倒计时

        

     function todou(n){
                              if(n<10){return '0'+n;}
                              else{return ''+n;}
                          }
    
        window.onload=function(){
                    var oBox=document.getElementById("box");
                    var oDate=new Date();      //未来时间
                         oDate.setFullYear(2016,9,1);  //把时间设置到2016年10月1日;
                          oDate.setHours(0,0,0,0);//把时间设置到0时0分0秒0毫秒;必须要设置;     
                function tick(){
                    var New=new Date();       //当前时间,必须放在tick()函数里面;
                    var ms=oDate.getTime()-New.getTime();//未来时间戳-现在时间戳毫秒数;不需要加parseInt;
                    //毫秒转为秒
                    var s=parseInt(ms/1000);
                    //秒转为天
                    var d=parseInt(s/86400);
                    //取小时
                    var h=parseInt(s%86400/3600);
                    var m=parseInt(s%86400%3600/60);
                    var s=parseInt(s%86400%3600%60);
                    oBox.innerHTML='距离十一还有:'+todou(d)+'天'+todou(h)+'时'+todou(m)+'分'+todou(s)+'秒';
                }
                tick();//防止开始为空
                setInterval(tick,1000)
                }
  • 相关阅读:
    315前夜
    学习,真正地学习
    华仔成都2007
    如果建筑师必须如网页设计师一般工作[转]
    “散文”
    笨小孩
    幸存者游戏启示[摘录]
    通过配置获取客户端所属服务器IP或服务器名
    观《三国之见龙卸甲》
    散文(二)
  • 原文地址:https://www.cnblogs.com/yang0902/p/5699563.html
Copyright © 2020-2023  润新知