Skip to content

Instantly share code, notes, and snippets.

@miyaoka
Last active May 15, 2018 00:03
Show Gist options
  • Save miyaoka/b37254f01393e84abdaa628587a9cc54 to your computer and use it in GitHub Desktop.
Save miyaoka/b37254f01393e84abdaa628587a9cc54 to your computer and use it in GitHub Desktop.
presentation timer
let sec = 60 * 8
const alertSec = 60
let timer
let alertTimer
const d = document.createElement('div')
const pad = (s) => `00${s}`.slice(-2)
const update = () => {
d.innerText = `${pad(Math.floor(sec / 60))} : ${pad(sec % 60)}`
}
const stop = () => {
void [timer, alertTimer].forEach((t) => {
clearInterval(t)
t = undefined
})
}
const toggleTimer = () => {
if (timer) {
stop()
return
}
timer = setInterval(() => {
sec -= 1
if (sec < alertSec && !alertTimer) {
alertTimer = setInterval(() => {
d.style.color = '#' + (((1 << 24) * Math.random()) | 0).toString(16)
}, 10)
}
if (sec <= 0) {
stop()
}
update()
}, 1000)
}
d.style.cssText = `
position: fixed;
font-size: 48px;
right: 10px;
top: 10px;
line-height: 1;
color: #fff;
text-shadow: 0 0 8px #000;
z-index: 2147483647;
cursor: pointer;
font-family: 'Passion One', cursive;
`
d.addEventListener('click', toggleTimer)
$('body').append(d)
$('head').append(
`<link href='https://fonts.googleapis.com/css?family=Passion+One' rel='stylesheet' type='text/css'>`
)
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment