因为主题自己个人改造美化的需要,每次加载耗时会影响页脚排版,因此将加载耗时打印到控制台以供查看。
因为开启了 Pjax ,所以想每次都打印,就需要把 由于不熟悉 PHP ,因此还是打算把代码放出来进行讨论,Pjax 执行回调之后,只会打印的是第一次打开的页面的 PHP 加载时间,没有办法打印当前页面的加载时间。pjax_debug_to_console(timer_stop())
放到 Pjax 回调函数里面。
看来得补习一下 PHP 了 〒▽〒
如果没有开启 Pjax 的需要使用 debug_to_console(timer_stop())
就行了。
创建 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;
}
function debug_to_console( $data ) {
$output = $data;
echo "<script>console.log( '加载耗时: " . $output . "' );</script>";
}
function pjax_debug_to_console( $data ) {
$output = $data;
echo "console.log( '加载耗时: " . $output . "' );";
}
单次打印加载耗时
<?php debug_to_console(timer_stop()) ?>
Pjax 回调函数中使用
<?php pjax_debug_to_console(timer_stop()) ?>
这个方法不太严谨,只能用来美化博客,再用js输出到控制台还会有差异,php的打印函数是var_dump(),你要是想查看耗时的话可以把函数放在根目录index.php里打印看看,但最好是用其他工具更准确。
Pluto 2021-11-26
THX (ღ′◡‵)
Kurococ Liu 2021-12-03 回复 @Pluto