View Full Version : Timer of how long user has been at page

name _F1
08-27-2006, 04:22 AM
I'm trying to make a timer that displays how long a user has stayed at the page. I wish to display the time in the format hh:mm:ss.

function timeAtPage() {
if (started == 0) {
startingTime = new Date();
startingHour = startingTime.getHours();
startingMin = startingTime.getMinutes();
startingSec = startingTime.getSeconds();
started = 1;
currentTime = new Date();
currentHour = currentTime.getHours();
currentMin = currentTime.getMinutes();
currentSec = currentTime.getSeconds();
timeHour = currentHour - startingHour;
timeMin = currentMin - startingMin;
timeSec = currentSec - startingSec;
document.clockForm.timeatpage.value = timeHour+":"+timeMin+":"+timeSec;
clockTimer = setTimeout('timeAtPage()',1000);
This does not change the value of the text box at all. Is this the best way of displaying the time, and what is wrong with my code?

Any help would be greatly appreciated. :)

08-27-2006, 05:10 AM

The following script can be found at http://www.javascript.internet.com/time-date,
It does tell you how long you have been there for but only when you leave and I suppose with a few changes it could be in a form. Also I'm not sure how to set it out like you said but if you want I could find out? Just let me know.

<script language="javascript" type="text/javascript">
var time=1;
function timeHere() { time = time + 1; finalTime = time / 10; }
function sayTime() {
finalTime = time / 10; alert("You were on this page for " + finalTime + " seconds!"); }
<body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()">