Skip to content

Commit

Permalink
Update time right away
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasvirkus committed Dec 2, 2023
1 parent 2889b25 commit 933a5b8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 51 deletions.
26 changes: 0 additions & 26 deletions public/sw-destroy.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ const { title, description, image = '/blog-placeholder.jpg' } = Astro.props
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={new URL(image, Astro.url)} />

<script is:inline src="/sw-destroy.js"></script>
<script is:inline src="/js/sw-destroy.js"></script>
30 changes: 6 additions & 24 deletions src/components/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ if (hour < 6) timeOfDay = 'night'

<footer>
<hr />
<p>Good {timeOfDay}! It's <time :datetime={today}>{hourFormatted}:{minFormatted}</time>.</p>
<p>
Good <span class="js-time-of-day">{timeOfDay}</span>! It's <time :datetime={today}
>{hourFormatted}:{minFormatted}</time
>.
</p>

<div class="links">
<a href="mailto:[email protected]"> E-mail me! Write about anything.</a>
Expand All @@ -27,29 +31,7 @@ if (hour < 6) timeOfDay = 'night'
</div>
</footer>

<script type="module">
const datetime = document.querySelector('time')

function updateTime() {
if (!datetime) return

const today = new Date()
const hour = today.getHours()
const hourFormatted = hour < 10 ? '0' + hour : hour
const min = today.getMinutes()
const minFormatted = min < 10 ? '0' + min : min
let timeOfDay = hour < 12 ? 'morning' : hour < 18 ? 'afternoon' : 'evening'

if (hour < 6) timeOfDay = 'night'

const time = `${hourFormatted}:${minFormatted}`
const greeting = `Good ${timeOfDay}! It's ${time}.`
datetime.textContent = greeting
datetime.setAttribute('datetime', today.toISOString())
}

setInterval(updateTime, 30 * 1000)
</script>
<script is:inline type="module" src="/js/update-time.js"></script>

<style>
footer {
Expand Down

0 comments on commit 933a5b8

Please sign in to comment.