Shamsher Nawaz
Nov 17th 2006, 8:12 am
Look at the code. its just a stopwatch. Everything work fine. But the problem is the startTime will not initialize at when I press the start button. Instead of its only initialize at the time when I load the page. I had already tried startTime = startTime.getTime()
Here is the code:
<html>
<head>
<SCRIPT LANGUAGE="javascript">
var startTime = new Date();
var cTimer;
// This function just display the difference
// between two times and working fine...
function startTimer()
{
var currentTime = new Date();
currentTime.getTime();
t = (currentTime - startTime)/100;
min = parseInt(t/600);
t = t % 600;
sec = parseInt(t/10);
dec = parseInt(t % 10);
document.myForm.timeCounter.value = min.toString()+':'+sec.toString()+'.'+dec.toString();
cTimer=setTimeout("startTimer()",100);
}
function setTimer(obj)
{
if(obj.value=="Start")
{ startTime.getTime(); obj.value='Stop'; startTimer();}
else
{ obj.value = "Start"; clearTimeout(cTimer);}
}
</SCRIPT>
</head>
<Body>
<Form name="myForm">
<input type="text" name="timeCounter" value="00:00.00" size=8>
<input type="button" name="stopWatch" value="Start" onclick="setTimer(this)"><br><br>
</Form>
</Body>
</html>
Here is the code:
<html>
<head>
<SCRIPT LANGUAGE="javascript">
var startTime = new Date();
var cTimer;
// This function just display the difference
// between two times and working fine...
function startTimer()
{
var currentTime = new Date();
currentTime.getTime();
t = (currentTime - startTime)/100;
min = parseInt(t/600);
t = t % 600;
sec = parseInt(t/10);
dec = parseInt(t % 10);
document.myForm.timeCounter.value = min.toString()+':'+sec.toString()+'.'+dec.toString();
cTimer=setTimeout("startTimer()",100);
}
function setTimer(obj)
{
if(obj.value=="Start")
{ startTime.getTime(); obj.value='Stop'; startTimer();}
else
{ obj.value = "Start"; clearTimeout(cTimer);}
}
</SCRIPT>
</head>
<Body>
<Form name="myForm">
<input type="text" name="timeCounter" value="00:00.00" size=8>
<input type="button" name="stopWatch" value="Start" onclick="setTimer(this)"><br><br>
</Form>
</Body>
</html>