javascript页面显示时钟并自动刷新

页面代码:clock.html

点击(此处)折叠或打开

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>时钟</title>
  6. </head>
  7. <body>
  8.     <span id="clock"> </span>
  9. </body>
  10. <script type="text/javascript" src="clock.js" charset=“utf-8”></script>
  11. </html>
对应的js文件:clock.js

点击(此处)折叠或打开

  1. /**
  2.  * 时间显示
  3.  */
  4. function Clock() {
  5.     var date = new Date();
  6.     this.year = date.getFullYear();
  7.     this.month = date.getMonth() + 1;
  8.     this.date = date.getDate();
  9.     this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
  10.     this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  11.     this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  12.     this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

  13.     // 日期显示
  14.     this.toString = function() {
  15.         return "日期:" + this.year + "-" + this.month + "-" + this.date + "-" + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
  16.     };

  17.     this.toSimpleDate = function() {
  18.         return this.year + "-" + this.month + "-" + this.date;
  19.     };

  20.     this.toDetailDate = function() {
  21.         return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;
  22.     };

  23.     this.display = function(ele) {
  24.         var clock = new Clock();
  25.         ele.innerHTML = clock.toString();
  26.         window.setTimeout(function() {
  27.             clock.display(ele);
  28.         }, 1000);
  29.     };
  30. }

  31. var clock = new Clock();
  32. clock.display(document.getElementById("clock"));
显示效果:


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12060579/viewspace-1799891/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12060579/viewspace-1799891/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值