在JavaScript中计算页面加载时间

在JavaScript中计算页面加载时间,第1张

在JavaScript中计算页面加载时间

永远不要使用

setInterval
setTimeout
功能进行时间​​测量!它们不可靠,很有可能在文档解析和显示期间JS执行调度被延迟。

相反,在页面开始加载时,使用该

Date
对象创建时间戳记,并计算页面完全加载时的时间差:

<doctype html><html>    <head>        <script type="text/javascript"> var timerStart = Date.now();        </script>        <!-- do all the stuff you need to do -->    </head>    <body>        <!-- put everything you need in here -->        <script type="text/javascript">  $(document).ready(function() {      console.log("Time until DOMready: ", Date.now()-timerStart);  });  $(window).load(function() {      console.log("Time until everything loaded: ", Date.now()-timerStart);  });        </script>    </body></html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5620850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存