JS实现简单的实时时间显示
2018-02-28 本文已影响0人
一个NPC
通过JS实现在页面中显示当前的时间和显示停留在页面的时间。
<script type="text/javascript">
function time() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var house = date.getHours();
var minutes = date.getMinutes();
var second = date.getSeconds();
var milsecond = date.getMilliseconds();
document.getElementById("showtime").innerHTML = year + "-" + month + "-" + day + " " + house + ":" + minutes + ":" + second + ":" + milsecond;
setTimeout("time()", 100);
}
var timestart = new Date();
function overtime() {
var tt = new Date();
var ttt = new Date(tt.getTime() - timestart.getTime());
var milsecond1 = ttt.getMilliseconds();
var second1 = ttt.getSeconds();
var minutes1 = ttt.getMinutes();
document.getElementById("showtime1").innerHTML =minutes1+"分"+":"+second1+"秒"+":"+milsecond1+"毫秒";
setTimeout("overtime()", 100);
}
</script>
<body onload="time(),overtime()">
<div class="top">
<font>系统当前的时间:</font>
<font id="showtime"></font><br>
<font>页面停留的时间:</font>
<font id="showtime1"></font>
</div>
</body>
运行结果:
20160813163350061.jpg
注:本文引用自“小咖奋斗史-CSDN博客”