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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer error

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I've found a script that works as a timer and was wondering if someone could give me a hand changing it a little. The problem is that if you click the start button and it counts up to 10 then you push the stop button that works fine. However if you push the start button again instead of continuing from 10 it goes back to 0 then starts again.

Thanks very much

Ed

<script language="JavaScript">

<!--
// please keep these lines on when you copy the source
// made by: Nicolas -
var timerID = 0;
var tStart = null;

function UpdateTimer() {
if(timerID) {
clearTimeout(timerID);
clockID = 0;
}

if(!tStart)
tStart = new Date();

var tDate = new Date();
var tDiff = tDate.getTime() - tStart.getTime();

tDate.setTime(tDiff);

document.theTimer.theTime.value = ""
+ tDate.getMinutes() + ":"
+ tDate.getSeconds();

timerID = setTimeout("UpdateTimer()", 1000);
}

function Start() {
tStart = new Date();

document.theTimer.theTime.value = "00:00";

timerID = setTimeout("UpdateTimer()", 1000);
}

function Stop() {
if(timerID) {
clearTimeout(timerID);
timerID = 0;
}

tStart = null;
}

function Reset() {
tStart = null;

document.theTimer.theTime.value = "00:00";
}

//-->

</script>
<body onload="Reset()" onunload="Stop()">
<center><form name="theTimer"><table>
<tr>
<td colspan=3 align=center>
<input type=text name="theTime" size=5>
</td>
</tr>
<tr><td></td></tr>
<tr>
<td>
<input type=button name="start" value="Start" onclick="Start()">
</td>
<td>
<input type=button name="stop" value="Stop" onclick="Stop()">
</td>
<td>
<input type=button name="reset" value="Reset" onclick="Reset()">
</td>
</tr>
</table></form></center>
 
Code:
var timerID = 0;
var gseconds = 0;
function UpdateTimer() {
	gseconds += 1;
	timerID = clearTimeout(timerID);
	document.theTimer.theTime.value = gseconds;	
	Start();
}
function Start() {
	document.getElementById("startButon").disabled = true;
	tStart	= new Date();
	timerID  = setTimeout("UpdateTimer()", 1000);
}
function Stop() {
	document.getElementById("startButon").disabled = false;
	if(timerID) {
		clearTimeout(timerID);
		timerID  = 0;
	}
}
function Reset() {
	document.getElementById("startButon").disabled = false;
	document.theTimer.theTime.value = "0";
	clearTimeout(timerID);
	timerID  = 0;
	gseconds = 0;
}
Do something like this.....(it's not fully tested but you get the idea.
 
Excellent thanks this has fixed the main problem. However now it is counting up as counter rather than timer so it says 70 instead of 1:10.

Is this easily fixed or would a approach be required?

Thanks again

Ed
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
<script language="JavaScript">
// please keep these lines on when you copy the source
// made by: Nicolas - [URL unfurl="true"]http://www.javascript-page.com[/URL]

var timerID = 0;
var tStart  = null;

var timerID = 0;
var gseconds = 0;
function UpdateTimer() {
    gseconds += 1;
    timerID = clearTimeout(timerID);
    document.theTimer.theTime.value = converTime(gseconds);

    Start();
}
function Start() {
    document.getElementById("startButton").disabled = true;
    tStart    = new Date();
    timerID  = setTimeout("UpdateTimer()", 1000);
}
function Stop() {
    document.getElementById("startButton").disabled = false;
    if(timerID) {
        clearTimeout(timerID);
        timerID  = 0;
    }
}
function Reset() {
    document.getElementById("startButton").disabled = false;
    document.theTimer.theTime.value = "0";
    clearTimeout(timerID);
    timerID  = 0;
    gseconds = 0;
}
function converTime(seconds){
	h = Math.floor(seconds/3600);
	m = Math.floor((seconds % 3600)/60);
	s = Math.floor((seconds % 3600) % 60);
	return(h+':'+m+':'+s);
}

</script>
<body onload="Reset()" onunload="Stop()">
<center><form name="theTimer"><table>
   <tr>
      <td colspan=3 align=center>
         <input type=text name="theTime" size=5>
      </td>
   </tr>
   <tr><td></td></tr>
   <tr>
      <td>
         <input type=button id="startButton" name="start" value="Start" onclick="Start()">
      </td>
      <td>
         <input type=button name="stop" value="Stop" onclick="Stop()">
      </td>
      <td>
         <input type=button name="reset" value="Reset" onclick="Reset()">
      </td>
   </tr>
</table></form></center>
 </body>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top