Solved

Javascript/PHP Timer?

Posted on 2011-03-06
3
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.

636 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