From e0a69eb031316ae145f1c40319bceb8ec520ad44 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Tue, 28 Feb 2023 21:54:53 -0800 Subject: init project structure --- assets/site.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 assets/site.js (limited to 'assets/site.js') diff --git a/assets/site.js b/assets/site.js new file mode 100644 index 0000000..4e72511 --- /dev/null +++ b/assets/site.js @@ -0,0 +1,32 @@ +function settime() { + const timestamp = document.querySelector('[data-timestamp-text]') + if (!timestamp || !('Intl' in window)) return + + const options = { + timeZone: "Asia/Taipei", + timeStyle: "short", + hour12: false + } + + // https://gist.github.com/muan/e7414b6241f088090acd916ed965540e + let time = new Intl.DateTimeFormat(navigator.language || "zh-TW", options).format(new Date()) + + // https://bugs.chromium.org/p/chromium/issues/detail?id=1262801 + if (time.match(/^24:/)) time = time.replace('24:', '00:') + + // Setting interpolated string instead of just the time because + // if there's no JS there should be no mentions of current time + const text = timestamp.getAttribute('data-timestamp-text').replace('{time}', time) + timestamp.innerHTML = text.replace(':', ':') + + const now = new Date() + const sec = now.getSeconds() + const secondIsEven = sec % 2 === 0 + const colon = document.querySelector('[data-colon]') + if (colon) colon.style.animationDelay = `${(secondIsEven ? 0 : 1000) - now.getMilliseconds()}ms` + + const delay = 60000 - ((sec * 1000) + now.getMilliseconds()) + setTimeout(settime, delay) +} + +settime() -- cgit v1.2.3