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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
brigmarConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
 
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
All Courses

From novice to tech pro — start learning today.