Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Counter

Status
Not open for further replies.

kyern

Programmer
Mar 30, 2006
36
US
I have a strange issue with a counter I use to find out how long is spent on a form. On the majority of computers I have checked this works fine, on other computers it counts two times faster than normal and will only update the seconds portion instead of cascading to minutes. All computers run the same os and have similar specs, the script still does it when cache is cleared. Any suggestions would be helpful.

Code:
var counter = 0;
var _myTimer_ms = null;
var _myTimer_s  = null;
var _myTimer_m  = null;
var _myTimer_h  = null;

function updateMS() { document.tracker.mssec.value = (100+parseInt(document.tracker.mssec.value)) % 1000; }
function updateS()  { document.tracker.sec.value   = (1+parseInt(document.tracker.sec.value))    % 60;   }
function updateM()  { document.tracker.tmin.value  = (1+parseInt(document.tracker.tmin.value))   % 60;   }
function updateH()  { document.tracker.hour.value  = (1+parseInt(document.tracker.hour.value));          }

function startIt() {
stopTimers();
resetTime();
_myTimer_ms = setInterval("updateMS()",100);
_myTimer_s  = setInterval("updateS()",1000);
_myTimer_m  = setInterval("updateM()",1000*60);
_myTimer_h  = setInterval("updateH()",1000*60*60);  
}

function startItAgain() {
_myTimer_ms = setInterval("updateMS()",100);
_myTimer_s  = setInterval("updateS()",1000);
_myTimer_m  = setInterval("updateM()",1000*60);
_myTimer_h  = setInterval("updateH()",1000*60*60);  
}

function stopTimers() {
clearInterval(_myTimer_ms);
clearInterval(_myTimer_s);
clearInterval(_myTimer_m);
clearInterval(_myTimer_h);
}

function resetTime() {
document.tracker.mssec.value=0;
document.tracker.sec.value=0;
document.tracker.tmin.value=0;
document.tracker.hour.value=0;
}

startIt() is called onload

html:

<input onclick="startItAgain();" type="button" value="Start" style="font-size: 10px;"> 
&nbsp;&nbsp;&nbsp;&nbsp;<input onclick="stopTimers();" type="button" value="Stop" style="font-size: 10px;"> 
<BR>	
Hours:&nbsp;&nbsp;&nbsp;&nbsp;<input style="font-size: 10px; text-align: right;" id="counter" SIZE="4"  onfocus="blur();" name="hour">&nbsp;
Minutes:&nbsp;&nbsp;&nbsp;&nbsp;<input style="font-size: 10px; text-align: right;" id="counter" size="4" onfocus="blur();" name="tmin">&nbsp;
Seconds:&nbsp;&nbsp;&nbsp;&nbsp;<input style="font-size: 10px; text-align: right;" id="counter" SIZE="4" onfocus="blur();" name="sec">&nbsp;
<input id="counter" style="visibility: hidden" onfocus="blur();" name="mssec">
 
I think your independent setInterval for ms, sec, min and hour will cause trouble no end. [1] Error of time difference between stopping and restarting will accumulate; [2] clearing multiple time interval handle may be another factor causing all sort of harzardous effects and error accumulation... (whether the clearInterval is clear one after another orderly is to look into, I suspect, it isn't.)

Rather to sauvage the situation, I would strong suggest all that functionality is handled by one and single setInterval. Like this.
[tt]
var counter = 0;
var _myTimer = null;
var _myInt=100; //millisecond; free to adjust

function updateTimer() {
var mssec=parseInt(document.tracker.mssec.value);
var sec=parseInt(document.tracker.sec.value);
var tmin=parseInt(document.tracker.tmin.value);
var hour=parseInt(document.tracker.hour.value);

var ntmp;
ntmp=Math.floor((mssec+_myInt)/1000);
if (ntmp>0) {sec+=ntmp;}
ntmp=Math.floor(sec/60);
if (ntmp>0) {tmin+=ntmp;}
ntmp=Math.floor(tmin/60);
if (ntmp>0) {hour+=ntmp;}

document.tracker.mssec.value=(_myInt+mssec)%1000;
document.tracker.sec.value=sec%60;
document.tracker.tmin.value=tmin%60;
document.tracker.hour.value=hour;
}

function startIt() {
stopTimers();
resetTime();
_myTimer = setInterval("updateTimer()",_myInt);
}

function startItAgain() {
_myTimer = setInterval("updateTimer()",_myInt);
}

function stopTimers() {
clearInterval(_myTimer);
}

function resetTime() {
document.tracker.mssec.value=0;
document.tracker.sec.value=0;
document.tracker.tmin.value=0;
document.tracker.hour.value=0;
}
[/tt]
The body form can remain the same. (Only you have plenty of elements with the same id. It will cost you. Better make them unique at this stage.) And I think it will solve all the problem of synchronization... and conceptually satisfying and the script with more neatness and conciseness.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top