Typecho调用加载时间

   AI摘要
本文介绍了如何在 Typecho 主题中添加页面加载时间显示功能。通过在 `Functions.php` 中添加代码,可以启动和停止计时器,并在前台 `footer.php` 中调用 `timer_stop()` 函数来显示加载时间。还可以使用美化代码将加载时间显示为带有绿色徽章的图像。
由Google Gemini Pro生成

在主题的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;
}

前台调用
footer.php合适位置插入

<?php echo timer_stop();?>

或使用美化

<a href="javascript:(0)" id="pagetimes"></a>  
<script>
document.getElementById('pagetimes').innerHTML = '<img src="https://img.shields.io/badge/页面加载耗时:-<?php echo timer_stop();?>-green">';
</script>
CC BY-NC-SA 4.0 Deed | 署名-非商业性使用-相同方式共享
最后更新时间:2024-03-15 18:22:29