Solved

Javascript/PHP Timer?

Posted on 2011-03-06
3
244 Views
Last Modified: 2012-05-11
I have a an online learning module that is essentially different questions, one per page that the user answers and then when they submit the answer the php pass through page sends their answer to the database where their userid is stored.

I need to time how long it takes the user to answer each question, so basically how long they are on each page, and i need those results sent to the database, there is a field for each of the 10 questions, ques1time, ques2time..etc.

I found this script online which gets the time but displays it to the user.  I don't want them to see, is there a way to instead pass the time to the php page and have it sent to the database.  I'm open to any suggestions to achieve the results i'm looking for, i'm just new at code and experiencing some trial and error.
<!-- Paste this code into an external JavaScript file named: timePage.js  -->

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Cody Shaffer :: http://codytheking313.byethost11.com */

var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;
/* Remove the "//" below to display in the title bar
  the amount of time the visitor has been on the site.
  Be aware though, that it does tend to be a bit distracting. */
// document.title = finalTime+" seconds you been here for!";
}

function sayTime() {
  finalTime = time / 10;
  alert("Thank you for coming to my site! \n You have been here " + finalTime + " seconds!");
}



<!-- Paste this code into the HEAD section of your HTML document.
     You may need to change the path of the file.  -->

<script type="text/javascript" src="timePage.js"></script>



<!-- Paste this code into the BODY tag -->

<body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()">

Open in new window

0
Comment
Question by:newmie22
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 250 total points
ID: 35050556

Open in new window

To do this you could fill in a hidden input field in your form with the elapsed time, using javascript.  Time passed is in milliseconds.  Divide by 1000 to get seconds.  Add the hidden input field to your form.  This form is just for testing.



<script>
var currentTime = new Date();
var startTime = currentTime.getTime();

function getElapsedTime()
{
	var endTime = new Date();
	var elapsedTime = (endTime.getTime() - startTime);
	form1.elapsedTime.value = elapsedTime;
}
</script>



<form id=form1 target=_blank>
<input type=hidden name=elapsedTime>
<input type=submit onclick="getElapsedTime()">
</form>

Open in new window

0
 
LVL 6

Assisted Solution

by:GregArnott
GregArnott earned 250 total points
ID: 35050673
Fast alteration:
  var field = document.getElementById("questtime");

function timeHere() {
  time = time + 1;
  finalTime = time / 10;
  field.value = finalTime;
}

Open in new window

and in the body of the page have the html for a hidden input named questtime.
ie:
<input type="hidden" name="questtime" id="questtime" />

Open in new window


Remove onunload="sayTime()" from the body, and the sayTime() function as well as they'll no longer be needed.

The hidden field will now have the time in seconds updated every second hidden from view, and will submit that value as one hits submit to leave the page.
0
 

Author Closing Comment

by:newmie22
ID: 35050998
Great, thank you both for your help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now