下面是代码的更新版本,修复了因冗余时间签入而导致的额外零startTime()
:
JavaScript
function startTime() {
var d=new Date();
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
h = h % 12;
h = h ? h : 12; // the hour \'0\' should be \'12\'
var ampm = h >= 12 ? \'PM\' : \'AM\';
m = checkTime(m);
s = checkTime(s);
document.getElementById(\'timeis\').innerHTML =" "+h+":"+m+":"+s;
}
function checkTime(i) {
if ( i == 0 ) {
return "00";
}
if ( i < 10 ) {
i = "0" + i;
}
return i;
}
setInterval(function() {
startTime();
}, 500);
HTML
<a id="timeis" href="#"></a>