tags:

views:

47

answers:

3

How can I know how long the current page has been loaded using Jquery or Javascript? What function?

+1  A: 

When the page loads get the current timestamp:

var startTime = new Date().valueOf();

Then the page has been loaded for the current timestamp minus this value:

var loadedSeconds = (new Date().valueOf() - startTime) / 1000;

The time is in milliseconds so divide by 1000 to get seconds.

Greg
A: 

You can do a var start = new Date(); in the beginning of your <head> then, at the end of the <body>, do a new Date().getTime() - start.getTime()

Soufiane Hassou
A: 

Those solutions tell you how long it has taken a page to load.

To tell how long the page has been running for you could run a function that finds the time (using the methods the other guys have mentioned) displays the time, and then calls itself (after maybe a small pause).

This is going to be a fairly intensive process and I wouldn't suggest doing it on a site that many visitors, or one that requires other JS to be running.

EDIT - Actually if you make it a button that you click to call the function and don't have it calling itself that would stop any crazy overheads.

Toby