<script language="JavaScript">
<!--
// set initial values
var timerRunning = false
var timerID = null
// create instance of Date object representing current time
var initial = new Date()
// start timer

function st() {	

	// ask the user if to reset the timer	;
		if (confirm("Would you like to reset the timer?"))		
	// set global variable to new time		
		{initial = new Date();}	else {return}

	// set the button's label to "stop"	
		document.forms[0].general.value = "stop"	;
	// assign the stop function reference to the button's onClick event handler	
		document.forms[0].general.onclick = stop	;

	// assign milliseconds since 1970 to global variable	
		startTime = initial.getTime()	;
	// make sure the timer is stopped	
		stopTimer()	;
	// run and display timer	
		showTimer();
}

// set button to initial settings

function stop() {	
	// set the button's label to "start"	
		document.forms[0].general.value = "start"	;
	// assign the start function reference to the button's onClick event handler	
		document.forms[0].general.onclick = st;	
	// stop timer	
		stopTimer();
}
	// stop timer

function stopTimer() {	
	// if the timer is currently running	
		if (timerRunning)	{	
	// clear the current timeout (stop the timer)		
		clearTimeout(timerID)	;}
	// assign false to global variable because timer is not running	
		timerRunning = false
}

function showTimer() {	
	// create instance of Date representing current timer	
		var current = new Date()	;
	// assign milliseconds since 1970 to local variable	
		var curTime = current.getTime()	;
	// assign difference in milliseconds since timer was cleared	
		var dif = curTime - startTime	;	
	// assign difference in seconds to local variable	
		var result = dif / 1000	;
	// is result is not positive	
		if (result < 1)		{
	// attach an initial "0" to beginning		
		result = "0" + result		;}
	// convert result to string	
		result = result.toString()	;
	// if result is integer	
		if (result.indexOf(".") == -1)	{	
	// attach ".00" to end		
		result += ".00"	;}
	// is result contains only one digit after decimal point	
		if (result.length - result.indexOf(".") <= 2)	{	
	// add a second digit after point		
		result += "0"	;}
	// place result in text field	
		document.forms[0].display.value = result	;
	// call function recursively immediately (must use setTimeout to avoid overflow)	
		timerID = setTimeout("showTimer()", 0);	
	// timer is currently running	
		timerRunning = true;
}
// -->
</script>
</head>
<body>
<center><h1>A Stopwatch</h1></center>
<p><center>
<form>
<input type="text" name="display" value="" onfocus="this.blur()">
<input type="button" name="general" value="start" onclick="st()"> 
</form></center>

