[Javascript] Survey and elapsed time behind the scenes
Paul Novitski
paul at novitskisoftware.com
Thu Jun 3 23:21:51 CDT 2004
At 07:12 PM 6/3/2004, dev at qroute.net wrote:
>When the page that has the survey form loads, I plan to start a javascript
>timer behind the scenes and to measure time it takes for the user to respond
>all the questions. Of course, no one is going to know about it. ;-)
>
>At the end, right at onSubmit, a hidden field will be populated with the
>elapaed time info at that time so the elapsed time as well can be shipped
>from the client to the server.
My personal approach would be simply to time-stamp the start & finish and
pass those values to my server-side script. You could define the start as
either the body.onload() event or the first input field the user interacts
with, and the final event is the form.submit() event.
(If you're generating your page from a server-side script, you might
consider doing the elapsed time calculation entirely from there: download
the page with the BeginTime field pre-set and subtract that time from Now()
when the page is submitted. That would have the advantage of allowing
people to subimt surveys even if their browsers don't speak DOM or JavaScript.)
Below is some skeleton code. The elapsed time in milliseconds is EndTime -
BeginTime.
Paul
______________________________
<form name="SurveyForm" id="SurveyForm" action="...">
<input type="hidden" name="BeginTime" id="BeginTime" value="" />
<input type="hidden" name="EndTime" id="EndTime" value="" />
</form>
window.onload = StartTimer
function StartTimer()
{
if (!document.getElementById)
{
alert("Sorry, this survery requires a more modern
browser"
return
}
// time-stamp hidden field
TimestampHiddenField("BeginTime")
// set the onsubmit event
var oForm = document.getElementById("SurveyForm")
oForm.onsubmit = StopTimer
}
function StopTimer()
{
// time-stamp hidden field
TimestampHiddenField("EndTime")
// submit the form
return true
}
function TimestampHiddenField(argId)
{
var oField = document.getElementById(argId)
var dNow = new Date()
oField.value = dNow.getTime()
}
More information about the Javascript
mailing list