Count Time On Page

[Total: 0    Average: 0/5]

This example shows how to have the status bar show how long up to the hour, and down to the second.

Head Code:

<script> 
<!-- Hide from old browsers 
var hour=0;
var min =0;
var sec =0;

function timer() {
  var _status = "You have been on this page for: ";
  if (++sec>59) {
    if (++min>59) {
      hour++;
      min=0;
    }
   sec=0;
  }
  var _min=min>9?min:"0"+min;
  var _sec=sec>9?sec:"0"+sec;
  var _hour=hour>9?hour:"0"+hour;
  _status += _hour+":"+_min+":"+_sec;
  defaultStatus = _status;
}

var myTimer=null;
var ONE_SECOND = 1000;
function startTimer() {
  endTimer();
  myTimer = setInterval("timer()",ONE_SECOND);
}
function endTimer() {
  if (myTimer != null) {
    clearInterval(myTimer);
	myTimer = null;
  }
}
// --> 
</script> 

Body Code:

<!-- <body onLoad="startTimer()" onunload="endTimer()">  -->
<a href="javascript:startTimer()">Click here to start the timer if it did not start by itself</a>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *