Whats wrong with this. Please help....

Discussion in 'JavaScript' started by Shamsher Nawaz, Nov 17, 2006.

  1. #1
    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;
    
    
    //  [COLOR="SeaGreen"]This function just display the difference 
    //  between two times and working fine...[/COLOR]
        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")
               { [COLOR="Red"]startTime.getTime()[/COLOR]; 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>
    
    
    Code (markup):
     
    Shamsher Nawaz, Nov 17, 2006 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    ....................
     
    Logic Ali, Nov 17, 2006 IP
  3. Shamsher Nawaz

    Shamsher Nawaz Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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();
     
    Shamsher Nawaz, Nov 18, 2006 IP