stopWatch - debug?

i thought i'd write a javascript stopwatch just for the hell of it and have run across some errors and stuff. can anyone help?

here is my code

// stopwatch javascript code.

var gbCounting=false;
var gHours=0;
var gMinutes=0;
var gSeconds=0;
var gHundredths=0;
var gButton=1;

function startCounting(){
  gbCounting=true;
  var keepTime=setTimeOut(10,"counting();");
}

function stopCounting(){
  gbCounting=false;
}

function resetCounter(){
  gbCounting=false;
  document.StopWatch.Face.value="           ";
  gHours=0;                   // hh:mm:ss:hh
  gMinutes=0;
  gSeconds=0;
  gHundredths=0;
}

function counting(){
  if (gbCounting){
    gHundredths+=1;
    if (gHundredths==100){
      gHundredths=0;
      gSeconds+=1;
    }
    if (gSeconds==60){
      gSeconds=0;
      gMinutes+=1;
    }
    if (gMinutes==60){
      gMinutes=0;
      gHours+=1;
    }
    if (gHours==24){
      stopCounting();
    }
    document.StopWatch.Face.value=makeOutString();
    setTimeout(10,"counting();");
} }

function makeOutString(){
  var outString="";
  if (gHours<10) outString = outString + "0";
  outString=outString + gHours + ":";
  if (gMinutes<10) outString = outString + "0";
  outString=outString + gMinutes + ":";
  if (gSeconds<10) outString = outString + "0";
  outString = outString + gSeconds + ":";
  if (gHundredths<10) outString = outString + "0";
  outString = outString + gHundredths;
  return outString;
}

function buttonFunctions(){
  if (gButton==1){
    startCounting();
    gButton+=1;
  }
  if (gButton==2){
    stopCounting();
    gButton+=1;
  }
  if (gButton==3){
    resetCounter();
    gButton=1;
  }
}


// use this code to insert JavaScript StopWatch into your html file
/*
<form name="StopWatch">
<script language="JavaScript" src="/stopWatch.js">
</script>
<input type="button" name="Face" value="           " onclick="buttonFunctions();"><br>
Press button to start, again to stop, again to reset.
</form>
*/


thanku
daniel
th_c_manAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brigmarCommented:
point 1:
it's.. setTimeout(expression, delay)
you have it back to front..

Brian
0
brigmarCommented:
point 2:
the logic of buttonFunctions()..

first iteration... the gButton will be 1... so it will do the first if() block.. which increases gButton to 2... which means it will do the second if() block.... etc etc

function buttonFunctions() {
 if (gButton==1) startCounting();
 if (gButton==2) stopCounting();
 if (gButton==3) resetCounter();
 gButton++;
 if (gButton == 4) gButton = 1;
}

Brian
0
brigmarCommented:
Try using the javascript Date object..

Here's a modified version of your script..

// stopwatch javascript code.

var gbCounting = false;
var startTime;
var gButton=1;

function startCounting() {
  gbCounting = true ;
  setTimeout( "counting()", 50);
  startTime = new Date();
}

function stopCounting(){
 gbCounting=false;
}

function resetCounter(){
 gbCounting=false;
 document.StopWatch.Face.value=" ";
}

function counting() {
 nowTime = new Date();
 elapsedTime = new Date(nowTime-startTime);
 gHours = elapsedTime.getHours();
 gMins = elapsedTime.getMinutes();
 gSecs = elapsedTime.getSeconds();
 gMill = elapsedTime.getMilliseconds();
 gHundredths = parseInt(gMill/10, 10);

  if (gHours==24 && gbCounting){
   buttonFunctions();
  }

  document.StopWatch.Face.value=makeOutString();
  if (gbCounting)
    setTimeout("counting();",50);
}

function makeOutString(){
 var outString="";
 if (gHours<10) outString = outString + "0";
 outString=outString + gHours + ":";
 if (gMins<10) outString = outString + "0";
 outString=outString + gMins + ":";
 if (gSecs<10) outString = outString + "0";
 outString = outString + gSecs + ":";
 if (gHundredths<10) outString = outString + "0";
 outString = outString + gHundredths;
 return outString;
}

function buttonFunctions() {
 if (gButton==1) startCounting();
 if (gButton==2) stopCounting();
 if (gButton==3) resetCounter();
 gButton++;
 if (gButton == 4) gButton = 1;
}



Brian
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

th_c_manAuthor Commented:
thanks for the help, i'll probably take this as an answer as soon as i get a chance to test it.
thanx for pointing out the if logic :) i didn't see that... lol. i always seem 2 get settimeout the wrong way around 2 lol...

daniel
0
brigmarCommented:
any luck ?
0
th_c_manAuthor Commented:
i haven't tested ur actual code yet, but i fixed the if logic and setTimeOut problem, and it was still spitting out errors. i'll get back asap. (gimme a couple of days)

daniel
0
th_c_manAuthor Commented:
i cant see why, but this is putting hours as 12 right from the start. i haven't let it go long ehough to find out what happens after an hour :).

i tested this at last, and tested mine, and mine runs now, but slowly - each tenth of a second is roughly 3/4 seconds. yours is mroe accurate. i'll give u the points, but first can you possibly tell me how its chucking a 12 for hours (im using NN4). if not, dont worry :)

daniel
0
brigmarCommented:
heheh.. thought this might be a problem.

I suspect you're in Australia/New Zealand, prolly GMT+12

change the relevant code as follows:

 gHours = elapsedTime.getUTCHours();
 gMins = elapsedTime.getUTCMinutes();
 gSecs = elapsedTime.getUTCSeconds();
 gMill = elapsedTime.getUTCMilliseconds();

oh yup... can see you're an Ozzie from your email address

Brian
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
th_c_manAuthor Commented:
Thank you very much, you've been a great help. Now I've just got to find a creative use for a javascript stopwatch. :)

Thanks,

Daniel
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.