MENU

添加站点生存时间的简单方法

June 7, 2020 • Read: 81 • 博客相关

引入js

将以下代码加入到<head>标签中。对于本主题,依次进入 控制台-外观-设置外观-主题自定义扩展,加入到 自定义HTML元素拓展-标签:head头部(meta 元素后),也可直接加入到主题对应的header.php中的</head>标签前。开始时间和中文提示可自定义。

<script>
    document.addEventListener('DOMContentLoaded', initLiveDay);
    function initLiveDay() {
        const birthTime = '2015/2/21 10:23:12';
        const template = (A, B, C, D) => `本站已勉强生存 ${A}天 ${B}时 ${C}分 ${D}秒.`;

        /* 锚点开始 */
        const container = footer.querySelector('.container');
        const p = document.createElement('p');
        container.insertBefore(p, container.firstElementChild);
        /* 锚点结束*/

        const msoad = 24 * 60 * 60 * 1000;
        const warp = n => n > 9 ? n : '0' + n;
        const toInt = n => warp(Math.floor(n));
        setInterval(() => {
            const lived = new Date() - new Date(birthTime);
            const days = lived / msoad;
            const intDays = toInt(days);
            const hours = (days - intDays) * 24;
            const intHours = toInt(hours);
            const minutes = (hours - intHours) * 60;
            const intMinutes = toInt(minutes);
            const seconds = (minutes - intMinutes) * 60;
            const intSeconds = toInt(seconds);
            p.innerHTML = template(intDays, intHours, intMinutes, intSeconds);
        }, 1000);
    }
</script>
Last Modified: June 28, 2020
Archives QR Code
QR Code for this page
Tipping QR Code
0:00