Simple timer using js

by barkkathulla 2013-07-19 18:23:06

1.Design part:

<body onload='timer();'>
<br><div>
<input type='text' id='disp' maxlength=12>
<button type='button' onclick='test()' id='test'>Initiate timer</button>
<button type='button' onclick='reset()' id='reset'>Stop timer</button>

2.Script:


var d=[0, 0, 0, 0, 0, 0, 0, 1];//timer loads

function test()
{
d[d[2]]=(new Date()).valueOf();
d[2]=1-d[2];

if (0==d[2])
{
clearInterval(t[4]);
d[3]+=d[1]-d[0];
var new = document.getElementById("lap");
var a = document.createElement("div");
a.id="div1";
a.innerHTML+="<table><tr><td width=65 align=center>"+
'Lap '+(d[7]++)+"</td><td width=116 align=center>"+
format(d[3])+"</td><td width=110 align=center>"+
format(d[1]-t[0])+"</td></tr></table>";
new.appendChild(a);
d[4]=d[1]=t[0]=0;
disp();
}
else
{
d[4]=setInterval(disp, 43);
}
}

function timer()
{
d[5]=new Date(1950, 1, 1, 0, 0, 0, 0).valueOf();
d[6]=document.getElementById('disp');
disp();
}

function reset()
{
d[4]=d[3]=d[2]=d[1]=d[0]=0;
document.getElementById('lap').innerHTML='';
ss();
disp();
d[7]=1;
}



function format(ms)
{
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 disp()
{
if (d[2]) d[1]=(new Date()).valueOf();
d[6].value=format(d[3]+d[1]-d[0]);
}
475
like
0
dislike
0
mail
flag

You must LOGIN to add comments