Seconds since last refresh

This script shows you time in seconds since a web page was last refreshed. Alternatively, that time could be thought of as for how long a visitor sees your page. For example, if it has been 20 seconds since the page was last refreshed, one can assume the visitor saw the page for 20 seconds.

To determine the time since last refresh, we use a counter variable that is updated every second. The same script could also be used whenever you want to execute some code every second (or based on some other time interval; simply change the second argument for setTimeout () method! This argument takes in time in milliseconds.) See the code below for full details.

First, the output of the script:

Time in seconds since this page was last refreshed:

Scripting code

Step 1: create a text box that we will use to display the number of seconds passed since the page was last refreshed. Place this code inside the body section (somewhere between <body> and </body>) of your web page:

<form name="LastRefreshFrm">
<h4>Time in seconds since this page was last refreshed: <input type="text"
          name="refreshSecBox" size="5" class="RefreshBox" /></h4>

<script language="javascript">
SecondsSinceLastRefresh ();

Step 2: now, we need to define a style (specifically, the class RefreshBox) that we will have specified above for the input box. This class will control the appearance (i.e., the font size, font alignment, etc.) for our text box. Place this code in the head section (between <head> and </head>) of your webpage:

.RefreshBox {
border:#990000 groove 2px;

Step 3: finally, we have to write the JavaScript code that will determine and display the time (in seconds) since the page was last refreshed. Like the styling code, place the following code inside the head second of your web page (make sure that you either place this code before the <style> tag or after </style> tag!):

<script language="javascript">
var pageRefreshCounter = 0;
function SecondsSinceLastRefresh () {
// add 1 to current value of pageRefreshCounter
// update our input box
document.LastRefreshFrm.refreshSecBox.value = pageRefreshCounter;
// call SecondsSinceLastRefresh () every second;
// note 1000 milliseconds = 1 second
// if you change 1000 to 60000 that will call the function every 1 minute.
// This will display the time in minutes since the page was last refreshed!
setTimeout("SecondsSinceLastRefresh ()", 1000);