English 中文(简体)
How can I know how long the current page has been loaded using Jquery or Javascript?
原标题:

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

最佳回答

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.

Example Code :-

<script>
//Getting Date When Page Started Loading
var start = new Date();

//Window Load Function that is called after page is completely loaded
$(window).load(function() {

//Substracting Started Time From Time When Page Completely Loaded
   $( body ).html(new Date() - start); 
});
</script>
问题回答

The Web Animations API provides document.timeline, which has a currentTime property with the age of the page (technically, it starts counting when you navigate away from the previous page).

console.log(`This page has existed for ${document.timeline.currentTime} milliseconds.`);

Note that this feature is pretty new. Chrome only added it in July of 2020, so if you need to support older browsers then your best bet is to use one of the other solutions provided on this page.

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()

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.





相关问题
How do I wait for a TTimer to finish?

I have a TFrame (fraDisplay) with a TTimer (timAnimateDataChange). The timer is used to control a small animation. In the form containing the frame I want to have a method that does something like ...

How to tell if an ajax timer has gone off at page_load

I have a web application and I m attempting to put an ajax timer control in it to give me a post back every 10-20 seconds. (possibly longer depending on performance). I have a lot of dynamically ...

Got an error 1120 ~ Access of undefined property

This is not your typical 1120. I know better than to have buttons/MCs without instance names on the timeline. Nope, this problem resides in a I timer I built from script I found online. The undefined ...

what is the right way to call the Timer ElapsedEventHandler

i have a timer that calls its even handler method every 30 seconds. but i want to initialy call this method. the signature of the event handler method is void TimerElapsed_GenerateRunTimes(object ...

Timer for a polling windows service

I wrote a Timer class in order to use it within a windows service that is polling another system. I did this because I had two issues that the System.Timers.Timer is not addressing. The Elapsed ...

Using C# Timer to stop executing a Program

I want a program to stop executing for a certain amount of time. And i want this to happen in regular intervals. For example, i want a program to run for 5 minutes and then it should stop for 2 ...

热门标签