Widget:Countdown:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
<includeonly> | <includeonly> | ||
<!--{assign var="targetTime" value=$time|default:$t}--> | |||
<span class="custom-countdown" | <span class="custom-countdown" | ||
data-target="<!--{$ | data-target="<!--{$targetTime|escape:'quotes'}-->" | ||
data-before="<!--{$before|default:'$1前'}-->" | data-before="<!--{$before|default:'$1前'|escape:'quotes'}-->" | ||
data-after="<!--{$after|default:'还剩$1'}-->">...</span> | data-after="<!--{$after|default:'还剩$1'|escape:'quotes'}-->">...</span> | ||
<script> | <script> | ||
(function() { | (function() { | ||
let | let timer = null; | ||
function | function update() { | ||
const now = new Date(); | const now = new Date(); | ||
document.querySelectorAll('.custom-countdown').forEach(el => { | document.querySelectorAll('.custom-countdown').forEach(el => { | ||
| 第15行: | 第16行: | ||
const afterTpl = el.getAttribute('data-after'); | const afterTpl = el.getAttribute('data-after'); | ||
if (!targetStr) return; | |||
const finalTargetStr = /^\d{4}$/.test(targetStr) ? targetStr + "-01-01T00:00:00" : targetStr; | const finalTargetStr = /^\d{4}$/.test(targetStr) ? targetStr + "-01-01T00:00:00" : targetStr; | ||
const target = new Date(finalTargetStr); | const target = new Date(finalTargetStr); | ||
| 第26行: | 第29行: | ||
const absDiff = Math.abs(diff); | const absDiff = Math.abs(diff); | ||
const days = Math.floor(absDiff / 864e5); | const days = Math.floor(absDiff / 864e5); | ||
const hours = Math.floor((absDiff % 864e5) / 36e5); | const hours = Math.floor((absDiff % 864e5) / 36e5); | ||
| 第46行: | 第48行: | ||
} | } | ||
function start() { if(! | function start() { if(!timer){ update(); timer=setInterval(update, 1000); } } | ||
function stop() { clearInterval( | function stop() { clearInterval(timer); timer=null; } | ||
document.addEventListener('visibilitychange', () => { document.hidden ? stop() : start(); }); | document.addEventListener('visibilitychange', () => { document.hidden ? stop() : start(); }); | ||
start(); | start(); | ||
2026年5月3日 (日) 15:28的版本
这是一个倒计时微件,仅供{{Countdown}}使用。