ITPub博客

首页 > 应用开发 > IT综合 > javascript页面显示时钟并自动刷新

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

原创 IT综合 作者:310248712 时间:2015-09-15 10:58:11 0 删除 编辑
页面代码: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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-07-17

  • 博文量
    18
  • 访问量
    28772