Skip to content

Instantly share code, notes, and snippets.

@Endle
Created August 10, 2016 05:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Endle/6776c147acda7d29ae7d2a5cb4f849c0 to your computer and use it in GitHub Desktop.
Javascript time count 简单的计时器代码
<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