设置网站运行时间

警告
本文最后更新于 2023-07-07,文中内容可能已过时。

使用javascript计算博客等网站的运行时间。

话不多说,直接贴码!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function createTime() {
  var now = new Date();
  var run = new Date('05/28/2018 20:01:01');
  //總的秒數
  var runTime = (now - run) / 1000,
    days = Math.floor(runTime / 60 / 60 / 24),
    hours = Math.floor(runTime / 60 / 60 - 24 * days),
    minutes = Math.floor(runTime / 60 - 24 * 60 * days - 60 * hours),
    seconds = Math.floor((now - run) / 1000 - 24 * 60 * 60 * days - 60 * 60 * hours - 60 * minutes);
  //前置零
  if (String(hours).length === 1) {
    hours = '0' + hours;
  }
  if (String(minutes).length === 1) {
    minutes = '0' + minutes;
  }
  if (String(seconds).length === 1) {
    seconds = '0' + seconds;
  }
  /*document.querySelector(".run-times").innerHTML = days + "  天 " + hours
          + "  时 " + minutes + "  分 " + seconds + "  秒";
  */
  document.querySelector('.run-times').innerHTML = 'RunTime: ' + days + ',' + hours + ':' + minutes + ':' + seconds + '';
}

//setInterval("createTime()", 500);
if (!document.hidden) {
  var siteTime = setInterval('createTime()', 500);
} else {
  clearInterval(siteTime);
}

/**
 * HTML 写法
<span class="run-times" title="网站运行时间">载入时分秒 ...</span>
<p class="run-times" title="主頁运行时间">RunTime Loading...</p>
*/
Buy me a coffee~
支付宝
微信
0%