javascript timer over multiple pages

Posted on 2011-03-09
Last Modified: 2012-08-14
I was shown the code below by another expert, and it's worked wonderfully for the parts where i needed to just send the time spent on one page to the database.

However, there are a few pages where it would be easiest that it keeps the time over a number of pages.  I have a learning section for this module/quiz where there are different pages of information they can review before taking the test questions.  I want to know how long they spend looking at those pages, they can move freely between the 5 pages until they click the finished link in the navigation.

Is there a way to accomplish what i'm looking to do? Thanks for any help.
var currentTime = new Date();
var startTime = currentTime.getTime();

function getElapsedTime()
	var endTime = new Date();
	var elapsedTime = (endTime.getTime() - startTime);
	form1.elapsedTime.value = elapsedTime;

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

Open in new window

Question by:newmie22
  • 3
  • 3
LVL 16

Expert Comment

ID: 35090015
How are you saving the partial answers from each page within the set of five until the user finally clicks the finished link?  Cookies?

Related question - How do you gather up the answers from all five pages into one form?

We need to (essentially) save the startTime variable along with the other partial answers.

Need to see more of your code.


Author Comment

ID: 35090058
the 5 pages that i want to gather time info for are just teaching pages, they is no user interaction besides reading, but for the purposes of comparison for the study, i want to see how long it takes the user to absorb the information.

Essentially there 4 pages with information like the code below and the navigation has a finished button/page where they'll leave the learning section and proceed to the 'test'

<!DOCTYPE html>
<html lang="en">
		<meta charset="utf-8">
        <meta name="viewport" content="width=device-width; height=device-height; initial-scale=1;">
        <title>Digital Natives Testing Module</title>
        <link rel="stylesheet" type="text/css" href="styles.css" />
        <!--[if IE]>
        <style type="text/css">
        .clear {
          zoom: 1;
          display: block;


    	<section id="page"> <!-- Defining the #page section with the section tag -->
            <div class="header"> <!-- Defining the header section of the page with the appropriate tag -->

                <h1>Test A</h1>
                <nav> <!-- The nav link semantically marks your main site navigation -->
                        <li><a href="testA-overheat.html">OVERHEAT</a></li>
                        <li><a href="testA-jammed.html">JAMMED</a></li>
                        <li><a href="testA-continue.html">CONTINUE</a></li>
                        <li><a href="testA-stop.html">STOP</a></li>
                        <li><a href="testA-finished.html">FINISHED</a></li>
            <section id="articles"> <!-- A new section with the articles -->

				<!-- Article 1 start -->

                <div class="line"></div>  <!-- Dividing line -->
                <div class="article" id="article1"> <!-- The new article tag. The id is supplied so it can be scrolled into view. -->
                    <div class="line"></div>
                    <div class="articleBody clear">
                    <div class="figure"> <!-- The figure tag marks data (usually an image) that is part of the article -->
	                    	<img src="img/a_jammed.png" alt="jammed" />
                      <p>If the machine gets a part stuck in the belt, this may damage the machine. When the machine displays a triangle, this indicates that it is jammed and the belt needs to be cleared.</p>
				<!-- Article 1 end -->


        <footer> <!-- Marking the footer section -->

          <div class="line"></div>
		</section> <!-- Closing the #page section -->

Open in new window

LVL 16

Accepted Solution

sjklein42 earned 500 total points
ID: 35090349
I think I have the answer.  It is a little hard to explain, but not really that complicated.

Here's my suggestion.  As the user goes from page to page, we pass the cumulative elapsed time as part of the URL (CGI argument).  So the links are dynamically patched to look like this:


The trick is, that the CGI argument is actually client-side only (browser) and can be tacked onto a plain old HTML page's URL without bothering the server.  Each of the four pages has javascript that looks to see if there is a "st" switch, and, if so, starts with that as the elapsed time instead of zero.

Then, when the "FINISH" link is taken, there is a CGI parameter ("st") that communicates the total elapsed time for the whole set of pages to the Finish CGI script, which logs it and goes onto the next part of the test.

To try this out, save multiple copies of this code as "testA-overheat.html", "testA-jammed.html", and so on for your four pages.  The Finish page is going to be different since that is when you want to record the total elapsed time.

Starting with any one of the pages, click the links to go from page to page and watch the URL in the address field.  It should show the cumulative elapsed time.

var currentTime = new Date();
var startTime;

var loc = window.location+"";
var xx = loc.split("?");
if (xx[1] != null) { startTime = currentTime.getTime() - xx[1].substr(3,10); }
else               { startTime = currentTime.getTime();                      }

function chainToPage()
    var endTime = new Date();
    var elapsedTime = (endTime.getTime() - startTime); = elapsedTime;

<form id=form1 action="testA-overheat.html"><input type=hidden name=st></form><a href=# onclick="chainToPage()">OVERHEAT</a>
<form id=form1 action="testA-jammed.html"><input type=hidden name=st></form><a href=# onclick="chainToPage()">JAMMED</a>
<form id=form1 action="testA-continue.html"><input type=hidden name=st></form><a href=# onclick="chainToPage()">CONTINUE</a>
<form id=form1 action="testA-stop.html"><input type=hidden name=st></form><a href=# onclick="chainToPage()">STOP</a>
<form id=form1 action="testA-finished.html"><input type=hidden name=st></form><a href=# onclick="chainToPage()">FINISHED</a>

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35090450
ok, i think i get what you're saying.  I see you have the code in the form tags.  The links i have in my code are <nav> tags with ul lists, would i need to change them to be form tags to get the timer results i'm looking to achieve?
LVL 16

Expert Comment

ID: 35090472
Yes, they need to be form tags to get the hidden attribute added to the URL.  The form itself is totally invisible and the a-href tag activates it, so the user is still clicking on a normal-looking link.

I think you can make it look the same on the page.


Author Closing Comment

ID: 35090542
Great, thanks for the help!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

713 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