This was a very interesting question, in terms of the difficulty in
deciding when the user starts filling up your form. Even if the first
item on your form is a radio button, the user may not start filling
your form with it. He/She may start by filling up the other form
elements and then come back to the radio button, or they may leave the
radio button at its default value, and never come back to it at all!
Clearly, the only way to accurately determine whether the user has
started filling up the form or not is to check all the form elements
and then start the timer when the user modifies the first one.
StopTimer() and ResetTimer().The StartTimer() function starts the
timer if it has not already been started. If the timer has already
been started, it does nothing. The StopTimer() function stops the
timer, and stores the time taken(in seconds) in the Hidden element of
the user form. It then disables the timer. The ResetTimer() function
just disables the timer, without storing its value in the hidden
element of the form.
Now, we must determine which form event to call the Starttimer()
function in. We have three choices: onFocus(), onClick() or
onChange(). From these, I have used onFocus() in my sample page, i.e.
I start the timer when any form element gains the focus. My reason for
using onFocus() was simple, it is supported by (nearly) all of the
form elements, while onChange() and onClick() are only supported by a
small subset of the form elements. You can of course, use any of these
events you determine as per your needs. In fact, you can even use
multiple events for each form element. All it would require is adding
the call to the StartTimer() function to that events handler. All the
elements of your form call the StartTimer() function, except the Reset
element and the Submit element. The Reset element will call the
ResetTimer() function, and the Submit element will call the
Another interesting anomaly I came across while selecting the event
handler is the password form element. According to Netscape's
and the onChange event handlers. However, I was unable to get my
Internet Explorer 6 [SP1] to execute any code in these event handlers
for the password element. This may be a bug in IE. You may have better
luck. What this means is, if the user starts filling your form by
clicking on the password element, the timer will NOT be started until
the user moves off to some other form element. Depending on your form
design and your desire for timer accuracy, this may not even be a
problem for you.
You can get the example HTML and JS files for the following locations:
[Example HTML] www31.brinkster.com/tanm/GA/Time.htm
[Example JS] www31.brinkster.com/tanm/GA/Time.js
In the example HTML page, click on any form element to start the
timer, and click on the Reset or Submit button to stop it. If you
click the submit button, it shows the time taken in seconds.
Before you start using this code in your page, you will have to make
some changes :
- In you HTML file, for your Form elements, you must add the
files. For more information on this, read the above explaination, and
check the example HTML file.
hidden element of the form[named myhiddenfield and myform
respectively], you must substitute the name of your form and your
- To help you test your pages, I have left a few message box calls in
calls. These calls have been marked in the code with the comment
For your convenience, I give the source code for the HTML page and the
*************** BEGIN HTML *****************
<FORM NAME="myForm" >
Button <INPUT TYPE="BUTTON" VALUE="Button"
CheckBox <INPUT TYPE="CheckBox" onfocus="StartTimer();">
File <INPUT TYPE="FILE" onfocus="StartTimer();" >
Hidden <INPUT TYPE="HIDDEN" name="myhiddenfield"
Password <INPUT TYPE="PASSWORD" NAME="myPass
onfocus="FormClicked();" onchange="StartTimer();" >
Radio <INPUT TYPE="RADIO" NAME="myRadio" value="Radio"
Text <INPUT TYPE="TEXT" NAME="myText"
Textarea <TEXTAREA NAME="myTextarea"
Select object <SELECT NAME="mySelect" onfocus="StartTimer();"
Reset object <INPUT TYPE="RESET" onfocus="ResetTimer();" >
Submit object <INPUT TYPE="SUBMIT" onclick="StopTimer();" >
*************** END HTML *******************
************* BEGIN Time.js ****************
var startTime=0, stopTime=0, timeTaken=0;
startTime = new Date();
startTime=startTime.getTime();// get time in milliseconds
timerStarted = true; //Timer Enabled
alert("Started Timer"); // REMOVE THIS
if( startTime ==0 )
// Change myForm to your Form name
// Change myhiddenfield to your hiddenfield name
alert("Time Taken == 0"); //REMOVE THIS
stopTime = new Date();
timeTaken = (stopTime - startTime)/(1000);
// Change myform to Your Form name
// Change myhiddenfield to your hiddenfield name
alert("Time Taken: "+window.document.myForm.myhiddenfield.value);
//Reset the Timer
//Reset Timer Info
timerStarted=false; // Disable Timer
alert("Timer Disabled"); //REMOVE THIS
*************** END Time.js *******************
Hope this helps.
If you need any clarifications, just ask!