永远不要使用
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>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)