侧边栏壁纸

Typecho开启页面测速,获取当前页面加载完成时间

2024年02月21日 115阅读 0评论 1点赞

判断当前页面加载是否快速,通常是直接在浏览器中访问网站,由自己的直观感受进行判断,而客观的方法则是计算具体的页面加载时间并显示出来。

  1. 在当前主题的functions.php文件添加代码:
function timer_start() {
        global $timestart;
        $mtime     = explode( ' ', microtime() );
        $timestart = $mtime[1] + $mtime[0];
        return true;
    }
    timer_start();
    function timer_stop( $display = 0, $precision = 3 ) {
        global $timestart, $timeend;
        $mtime     = explode( ' ', microtime() );
        $timeend   = $mtime[1] + $mtime[0];
        $timetotal = number_format( $timeend - $timestart, $precision );
        $r         = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s";
        if ( $display ) {
            echo $r;
        }
        return $r;
    }
  1. 在要显示加载时间的位置添加调用代码(通常为底部footer.php):
<p>页面加载:<?php echo timer_stop();?></p>
  1. 保存后,到前台刷新页面即可显示加载时间,效果看本站底部。
1

—— 评论区 ——

昵称
邮箱
网址
取消
博主栏壁纸
14 文章数
33 标签数
12 评论量