PDA

View Full Version : Javascript Timer


Nile
06-22-2008, 04:49 AM
I made a javascript timer when I was feeling bored, it should function right :thumbsup:. So here it is(blue=hour,red=minute,yellow=second):
Html Code
<html>
<head>
<title>Time down counter</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body onLoad="startCounter('counter',2,23,38);startCounter('counter1',5,13,9);"><button onClick="alert('Stopped,\n click OK to continue.');">Stop</button>
<div id="counter"></div>
<div id="counter1"></div>
</body>
</html>
Javascript Code(script.js)
function startCounter(putDiv,h,m,s){
var hh;
var mm;
var ss;
if(h == 0 && m == 0 && s == 1){
hh = 0;
mm = 0;
ss = 0;
document.getElementById(putDiv).innerHTML="<font color='red'><b><blink>"+hh+":"+mm+":"+ss+"</blink></b></font>";
alert("The timer is off!");
return false;
}
hh = h;
mm = m;
ss = s-1;
if(ss == 0){
ss = 59;
mm = m-1;
}
if(mm == -1 && hh != 0){
mm = 59;
hh = h-1;
}
if(h == 0 && m == 0 && s <= 6){
document.getElementById(putDiv).innerHTML="<font color='red'><b>"+hh+":"+mm+":"+ss+"</b></font>";
} else if(h == 0 && m == 0 && s <= 11){
document.getElementById(putDiv).innerHTML="<font color='red'>"+hh+":"+mm+":"+ss+"</font>";
} else {
document.getElementById(putDiv).innerHTML=hh+":"+mm+":"+ss;
}
setTimeout("startCounter('"+putDiv+"',"+hh+","+mm+","+ss+")", 1000);
}
[edit]Yes, I know you can make the start time something like this: startCounter('counter1',5,13,120);, lol.