How much time has elapsed, Since loading this web page?

admin

Administrator
Staff member
I have the javascript that How much time has elapsed,
Since loading this web page? it is OK.
But every time that i have been press Reload button at tool bar.
It will be start at zero (00:00:00).
I want to get the result to continue count the time.
The time should be continue not depend on Refresh bottom.
How to do that ?

####################################################
<html>
<head>
<title>How much time has elapsed, Since loading this web page?</title>

<Script Language="JavaScript">
// THESE TWO VARIABLES STORE THE TIME AND DATE WHEN THE PAGE IS LOADED
var startDate = new Date();
var startTime = startDate.getTime();

// THIS FUNCTION CALCULATES THE SECONDS ELAPSED SINCE THE PAGE WAS LOADED
function seconds_elapsed ()
{
var date_now = new Date ();
var time_now = date_now.getTime ();
var time_diff = time_now - startTime;
var seconds_elapsed = Math.floor ( time_diff / 1000 );

return ( seconds_elapsed );
}

// THIS FUNCTION TAKES THE SECONDS ELAPSED AND CONVERTS THEM FOR OUTPUT
function time_spent ()
{
// TAKE THE SECONDS ELAPSED
var secs = seconds_elapsed ();

// CONVERT SECONDS TO MINUTES AND SECONDS
var mins = Math.floor ( secs / 60 );
secs -= mins * 60;

// CONVERT MINUTES TO HOURS AND MINUTES
var hour = Math.floor ( mins / 60 );
mins -= hour * 60;

// DISPLAY THE FINAL OUTPUT TIME STRING
document.display.timeElapsed.value = pad ( hour ) + ":" + pad ( mins ) + ":" + pad ( secs );

// RECURSIVELY RE-RUN THE FUNCTION EVERY SECOND
setTimeout( "time_spent ()", 1000 );
}

// THIS FUNCTION INSERTS A LEADING ZERO (IF NECESSARY) TO PROVIDE UNIFORM OUTPUT
function pad ( num )
{
return ( ( num > 9 ) ? num : "0" + num );
}
</Script>
</head>

<body onLoad="time_spent()">

<form name="display">
<input name="timeElapsed" type="text" size=8>
</form>

</body>
</html>
####################################################
 
Back
Top