Jigsaw Puzzles - Reference And Education Articles - Anime Episodes - Internet Advertising - Debt Consolidation

PDA

View Full Version : Whats wrong with this. Please help....


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>

Logic Ali
Nov 17th 2006, 11:50 am
Here is the code:


<html>
<head>
<SCRIPT LANGUAGE="javascript">
var startTime = new Date();
var cTimer;

function startTimer()
{
var currentTime = new Date();
currentTime.getTime(); <-This line has no effect
t = (currentTime - startTime)/100;
min = parseInt(t/600);<- t is an integer, so all calls to parseInt are unnecessary
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';

That statement has no effect. Use: startTime=new Date();
....................

Shamsher Nawaz
Nov 18th 2006, 7:37 am
Yeh its true that parseInt() are used at wrong places.
Because I want to get the integer part of the resultent value and I don't
know any other method for doing this.

But why the getTime() is not effected. I have already
defined the variable on the top to making it Global. I Just want to initialize it
by current time value. How come I define it again with startTime=new Date();