Search code examples
jqueryloadtimer

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?


Solution

  • 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>