• date制作电子时钟(二)


    下面我们就让它自动刷新,就得要用到它的setTimeout函数了,//setInterval也行不过是有区别的哦。。。。

    View Code
     1 function Clock()
     2 {
     3   var date = new Date();
     4    var year = date.getFullYear();
     5    var month = date.getMonth();
     6    var day = date.getDate();
     7    var week = date.getDay();
     8    var hour = date.getHours();
     9    var minute = date.getMinutes();
    10    var second =  date.getSeconds();
    11    var millisecond = date.getMilliseconds();
    12    var flag = "AM" ;
    13    if( hour < 10){
    14      hour = "0"+hour; 
    15    }
    16    if(hour > 12){
    17      flag = "PM";
    18    }
    19    if(minute <10){
    20      minute = "0" + minute;
    21    }
    22    if(millisecond < 10){
    23     millisecond = "0" + millisecond;
    24    }
    25    switch(week){
    26         case 1:week = "一"; break;
    27         case 2:week = "二"; break;
    28         case 3:week = "三"; break;
    29         case 4:week = "四"; break;
    30         case 5:week = "五"; break;
    31         case 6:week = "六"; break;
    32         default: week = "日";break;
    33     }
    34    var result = year+"年 "+month+"月 "+day+"日  星期"+week+" "+hour+":"+minute+":"+second + " "+flag;
    35     document.getElementById("time").innerHTML=result;
    36     //有可能会只显示一会就停止了
    37     //setInterval("Clock()",1000);
    38     setTimeout("Clock()",1000);
    39 }
    40     
    41 </script>
    42 <style type="text/css">
    43 </style>
    44 </head>
    45 
    46 <body onload = "Clock()">
    47 <div id = "time"></div>
  • 相关阅读:
    利用window.onerror收集js代码异常
    js 基础题复习
    正则12和\1的理解
    CodeReview 方法 规范
    前端的登陆 几种类型
    http协议相关知识
    javascript中apply、call和bind的区别 详细易懂
    http常见的状态码,400,401,403 前端看
    vue中使用file-saver 下载各类文件
    js下载文件到本地各种方法总结
  • 原文地址:https://www.cnblogs.com/QLJ1314/p/2622949.html
Copyright © 2020-2023  润新知