Typecho添加一个当前页面加载完成速度时间

Typecho · 2023-04-02 · 79 人浏览

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

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;
}

2、在要显示加载时间的位置添加调用代码:

<?php echo timer_stop();?>

3、保存后,到前台刷新页面即可显示加载时间。

Typecho教程
【免责声明】 本网站大部分软件和资源收集于网络,只做自己私下学习备份使用。所有资源和软件均来自互联网的优秀作者们,版权归原作者或企业所有。