diff options
author | clarkzjw <[email protected]> | 2023-02-28 21:54:53 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-28 22:34:04 -0800 |
commit | e0a69eb031316ae145f1c40319bceb8ec520ad44 (patch) | |
tree | 6f436581b4c29b61ba4d82e09a7eecdc4f73fb24 /assets/site.js | |
parent | 6a77e4e0494e14fdbe1cb7847d2880132714d568 (diff) | |
download | photo-e0a69eb031316ae145f1c40319bceb8ec520ad44.tar.gz |
init project structure
Diffstat (limited to 'assets/site.js')
-rw-r--r-- | assets/site.js | 32 |
1 files changed, 32 insertions, 0 deletions
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 @@ | |||
1 | function settime() { | ||
2 | const timestamp = document.querySelector('[data-timestamp-text]') | ||
3 | if (!timestamp || !('Intl' in window)) return | ||
4 | |||
5 | const options = { | ||
6 | timeZone: "Asia/Taipei", | ||
7 | timeStyle: "short", | ||
8 | hour12: false | ||
9 | } | ||
10 | |||
11 | // https://gist.github.com/muan/e7414b6241f088090acd916ed965540e | ||
12 | let time = new Intl.DateTimeFormat(navigator.language || "zh-TW", options).format(new Date()) | ||
13 | |||
14 | // https://bugs.chromium.org/p/chromium/issues/detail?id=1262801 | ||
15 | if (time.match(/^24:/)) time = time.replace('24:', '00:') | ||
16 | |||
17 | // Setting interpolated string instead of just the time because | ||
18 | // if there's no JS there should be no mentions of current time | ||
19 | const text = timestamp.getAttribute('data-timestamp-text').replace('{time}', time) | ||
20 | timestamp.innerHTML = text.replace(':', '<span class="timestamp-colon" data-colon>:</span>') | ||
21 | |||
22 | const now = new Date() | ||
23 | const sec = now.getSeconds() | ||
24 | const secondIsEven = sec % 2 === 0 | ||
25 | const colon = document.querySelector('[data-colon]') | ||
26 | if (colon) colon.style.animationDelay = `${(secondIsEven ? 0 : 1000) - now.getMilliseconds()}ms` | ||
27 | |||
28 | const delay = 60000 - ((sec * 1000) + now.getMilliseconds()) | ||
29 | setTimeout(settime, delay) | ||
30 | } | ||
31 | |||
32 | settime() | ||