求一个js计时器:00:00:00,时间从秒开始,打开html页面后开始计时!
虽然前面已经有高手回答了, 可毕竟我也写了, 就贴上吧
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<style>
#mytime {
text-align: center;
}
</style>
<script>
function two_char(n) {
return n >= 10 ? n : "0" + n;
}
function time_fun() {
var sec=0;
setInterval(function () {
sec++;
var date = new Date(0, 0)
date.setSeconds(sec);
var h = date.getHours(), m = date.getMinutes(), s = date.getSeconds();
document.getElementById("mytime").innerText = two_char(h) + ":" + two_char(m) + ":" + two_char(s);
}, 1000);
}
</script>
</head>
<body onload="time_fun()">
<h1 id="mytime">00:00:00</h1>
</body>
</html>
效果图
<title>计时</title><meta name=viewport ,content="text/html,charset=utf-8; width=device-width,uc-fitscreen=yes" http-equiv="Content-Type">。
function getCurDate() {d=new Date(); var week=['天','一','二','三','四','五','六','天'];。(1900+d.getYear())+'年'+a0(d.getMonth()+1)+'月'+a0(d.getDate())+'日 星期'+week[d.getDay()]+' <a id=hms></a>'; Time.innerHTML=T; cont=0; dt()};。
function a0(x){if(x<10)return '0'+x; else return x}
function dt(){d=new Date()。hms.innerText=a0(d.getHours())+':'+a0(d.getMinutes())+':'+a0(d.getSeconds()); cont++; wt.innerHTML=cont; setTimeout("dt()",1000)},
<body onLoad='getCurDate()'>,
<div id='Time'></div><p><div style="font-,size:150%; border:solid 1 gray; height:3em">你已经在这个页面浪费了<a id=wt style=color:red></a>秒钟!</div>。
var HH = 0;
var mm = 0;
var ss = 0;
var str = '';
var timer = setInterval(function(){
str = "";
if(++ss==60)
{
if(++mm==60)
{
HH++;
mm=0;
}
ss=0;
}
str+=HH<10?"0"+HH:HH;
str+=":";
str+=mm<10?"0"+mm:mm;
str+=":";
str+=ss<10?"0"+ss:ss;
document.getElementById("d").innerHTML = str;
},1000);
};