Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer script

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I've found a stop watch script but am having trouble adapting it so that it doesn't show miliseconds. Can someone point me in the right direction please?

Thanks very much

Ed

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<html>
<head>
<title>Online Stopwatch</title>
<script type='text/javascript'>
// 0/1 = start/end
// 2 = state
// 3 = length, ms
// 4 = timer
// 5 = epoch
// 6 = disp el
// 7 = lap count

var t=[0, 0, 0, 0, 0, 0, 0, 1];

function ss() {
t[t[2]]=(new Date()).valueOf();
t[2]=1-t[2];

if (0==t[2]) {
clearInterval(t[4]);
t[3]+=t[1]-t[0];
document.getElementById('lap').innerHTML+=
'<tr><td>'+(t[7]++)+'</td><td>'+
format(t[1]-t[0])+'</td><td>'+
format(t[3])+'</td></tr>';
t[4]=t[1]=t[0]=0;
disp();
} else {
t[4]=setInterval(disp, 43);
}
}
function r() {
if (t[2]) ss();
t[4]=t[3]=t[2]=t[1]=t[0]=0;
disp();
document.getElementById('lap').innerHTML='';
t[7]=1;
}

function disp() {
if (t[2]) t[1]=(new Date()).valueOf();
t[6].value=format(t[3]+t[1]-t[0]);
}
function format(ms) {
// used to do a substr, but whoops, different browsers, different formats
// so now, this ugly regex finds the time-of-day bit alone
var d=new Date(ms+t[5]).toString()
.replace(/.*([0-9][0-9]:[0-9][0-9]:[0-9][0-9]).*/, '$1');
var x=String(ms%1000);
while (x.length<3) x='0'+x;
d+='.'+x;
return d;
}

function load() {
t[5]=new Date(1970, 1, 1, 0, 0, 0, 0).valueOf();
t[6]=document.getElementById('disp');

disp();

if (!window.opener && window==window.top) {
document.getElementById('remote').style.visibility='visible';
}
}

function remote() {
window.open(
document.location, '_blank', 'width=700,height=350'
);
return false;
}
</script>
<style type='text/css'>
#main button, #disp {
width: 8em;
vertical-align: middle;
}
#main button {
padding: 0.4em;
font-size: 1.1em;
}
#disp {
background-color: white;
font-size: 2em;
width: 7.25em;
font-family: "Courier New";
}

#main {
text-align: center;
white-space: nowrap;
}

#remote {
position: absolute;
top: 1px;
right: 1px;

visibility: hidden;
}

#lap {
margin-top: 0.5em;
}
</style>
</head>

<body onload='load();'>

<div id='main'>
<button type='button' onclick='ss()' onfocus='this.blur()'>Start / Stop</button>
<br>
<input type='text' id='disp' />
<br>
<button type='button' onclick='r()' onfocus='this.blur()'>Reset</button>
</div>


</body>
</html>
 
In the format function, comment out the line:

d+='.'+x ;

that will get rid of the milliseconds (from displaying).

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top