0

In my project i am using timer which runs, when start button is clicked and stop when end is clicked

The problem is by mistake when start button is clicked double or more than once ..time takes +1000 millsec extra and start running faster ..this should no happen

My javascript is..

<script type="text/javascript">
var sec = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
    sec++;
    if (sec == 60) {
        sec = 0;
        min = min + 1; 
    } else {
       min = min; 
    }

    if (min == 60) {
       min = 0; 
       hour += 1; 
    }
    if (sec<=9) { sec = "0" + sec; }
    document.getElementById("starttime").innerHTML = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" + min : min) + " : " + sec;
    SD=window.setTimeout("stopwatch();", 1000);
}

function f3() {
    clearTimeout(SD);
}
</script>
<form name="clock">
<div id="starttime" style="font-size:large; "></div>
<input type="button" name="theButton" id="Start" value="Start"  onClick="stopwatch(this.value);" />
<input type="button" name="theButton" id="end" value="Stop"  onClick="f3();" />
</form>

I have tried something like this..

  if (document.clock.theButton.value == "Start") {
            window.clearTimeout(SD);
            return true;  }`

.... but its not working

2 Answers 2

2

Don't call stopwatch() directly.

Call something else which checks SDs state, and then calls stopwatch().

<input type="button" name="theButton" id="Start" value="Start"  onClick="startStopWatch();" />

var sec = 0;
var min = 0;
var hour = 0;
var SD;

function startStopWatch(){
    if (!SD){
      stopwatch();   
    }
}

function stopwatch() {
   sec++;
  if (sec == 60) {
   sec = 0;
   min = min + 1; }
  else {
   min = min; }
  if (min == 60) {
   min = 0; 
   hour += 1; }
if (sec<=9) { sec = "0" + sec; }
  document.getElementById("starttime").innerHTML = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" + min : min) + " : " + sec;
 SD=window.setTimeout(stopwatch, 1000);
}

function f3() {
    if (SD){
       clearTimeout(SD);
    }
}

Live Demo: http://jsfiddle.net/uJPff/2

Sign up to request clarification or add additional context in comments.

4 Comments

@Tommi Have ammended my answer
thank u ...it working good ..i have one more problem...when stop button is pressed timer get stopped...then once again start button is pressed timer start from stopped value .the timer should not start
its working perfectly ...but i have to save stopped time(SD) to database how i can move value of stop time from script to home controller..
@GopalaKrishnan This would be a different question entirely. I would suggest reading up on AJAX, and post a new question if you run into any issues.
0

You can do something like this:

var SD;
function stopwatch(text) {
    if (SD && text) {
        return;
    }
            ....
}

function f3() {
    clearTimeout(SD);
    SD = false;
    sec = 0;
    min = 0;
    hour = 0;
}

I.e. you can check, if the timeout is already running when the button is clicked and cancel the execution, if needed.

You can test it at updated jsFiddle.

4 Comments

thank u ...it working good ..i have one more problem...when stop button is pressed timer get stopped...then once again start button is pressed timer start from stopped value .the timer should not start
You didn't tell where it should start, so I supposed this is was the default action. If you want to start from 0:0:0 again, just set all time variables to 0 in f3().
its working perfectly ...but i have to save stopped time(SD) to database how i can move value of stop time from script to home controller..
@GopalaKrishnan Well, you have a couple of working answers for the original question, please accept one of them. Then you better ask another question about database saving, in your original question database wasn't even mentioned...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.