使用Javascript查找网站的加载时间

时间:2018-11-17 15:15:49

标签: javascript page-load-time

我想找到本地托管网站的加载时间。这样有效吗?如何在对话框中显示时间?

<head>
<script type="text/javascript">
             var loadTime = window.performance.timing.domContentLoadedEventEnd- window.performance.timing.navigationStart;
        </script>
        </head>
        
<body>
<script type="text/javascript">
          window.onload = function () {
    var loadTime = ((window.performance.timing.domComplete- window.performance.timing.navigationStart)/1000)+" sec.";
    console.log('Page load time is '+ loadTime);
}
        </script>
</body>

1 个答案:

答案 0 :(得分:0)

@Samrat Shrestha此代码段为您服务

<doctype html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
    <head>
        <script type="text/javascript">
            $(document).ready(function() {
                 console.log("Time until DOMready: ",window.performance.timing.loadEventEnd-window.performance.timing.navigationStart);
             });
        </script>
        <!-- do all the stuff you need to do -->
    </head>
    <body>

    </body>
</html>

就绪事件在HTML文档加载后发生。 整个页面加载(图像,样式等)时触发window.onload

window.onload vs $(document).ready()

https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API#Examples

https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming/loadEventEnd