Created
August 10, 2016 05:48
Star
You must be signed in to star a gist
Javascript time count 简单的计时器代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript"> | |
var BEGIN_TIME=new Date(); | |
var HOUR=BEGIN_TIME.getHours(); | |
var MINUTE=BEGIN_TIME.getMinutes(); | |
var SECOND=BEGIN_TIME.getSeconds(); | |
var ID_ARRAY=[] | |
function startTime() | |
{ | |
update(); | |
BEGIN_TIME=new Date(); | |
HOUR=BEGIN_TIME.getHours(); | |
MINUTE=BEGIN_TIME.getMinutes(); | |
SECOND=BEGIN_TIME.getSeconds(); | |
t=setInterval(update,500); | |
} | |
// add a zero in front of numbers<10 | |
function update() | |
{ | |
var today=new Date(); | |
h=today.getHours(); | |
m=today.getMinutes(); | |
s=today.getSeconds(); | |
s = s - SECOND; | |
if (s<0) { s=s+60; m=m-1; } | |
m = m - MINUTE; | |
if (m<0) { m=m+60; h=h-1; } | |
h = h - HOUR; | |
m=checkTime(m); | |
s=checkTime(s); | |
ID_ARRAY.forEach(function(id){ | |
try{document.getElementById(id).innerHTML=h+":"+m+":"+s;} | |
catch(err){} | |
} | |
); | |
} | |
function checkTime(i) | |
{ | |
if (i<10) {i="0" + i;} | |
return i; | |
} | |
function start() | |
{ | |
startTime(); | |
ID_ARRAY.push("txt"); | |
ID_ARRAY.push("t2"); | |
ID_ARRAY.push("t3"); | |
//update(); | |
} | |
</script> | |
</head> | |
<body onload="start()"> | |
<div id="txt"></div> | |
t2 | |
<div id="t2"></div> | |
t3 | |
<div id="t3"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment