简体   繁体   中英

Display load time in html page

I want to display in the footer of my html page the time it takes to load(using javascript). But I noticed that the load time displayed on the page doesn't match the page load time shown in the browser.

Here is my javascript code:

(() => {
     const loadStart = new Date().getTime();

     window.addEventListener('load', () => {
         const timeStamp = document.querySelector('#timestamp');
         timeStamp.innerHTML += `Page load time: ${(new Date().getTime() - loadStart) / 1000} sec`;
     });
 })();

I connect the script in of the page and use the id of css-selector to display time.

The best solution I see is to use the JavaScript API but it's deprecated. I would like to use a more modern approach.

Try this

time.loadEventStart - time.navigationStart

from performance.timing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM