可说欢喜
Typecho

Typecho1.2 可用的页面加载时间代码

by 温柔, 2018-10-19


百度能搜索到的基本都用不了放到主题里什么都不输出这个是我从别的主题里面提取出来的代码记录一下
在主题 functions.php 文件里放入下面代码

    /**
     * 加载时间
     * @return bool
     */
    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;
    }

然后在主题foot.php文件需要放加载时间的地方添加加载耗时:<?php echo timer_stop();?>即可
效果示例:
20181019155636.png

typecho
温柔

作者: 温柔

1 条评论
    twlagkpo 回复
    twlagkpo2021-03-14 16:08

    十分感谢分享,用上了,感觉有点逼格了,哈哈,另外发现你的评论网址的前端正则校验好严啊,必须是带协议头(http/https),只写域名不给过

2024 © typecho & elise