Skip to content

Instantly share code, notes, and snippets.

@robwozniak
Last active August 24, 2018 17:40
Show Gist options
  • Save robwozniak/4c01fe0978d957225047ba48d7bdfb99 to your computer and use it in GitHub Desktop.
Save robwozniak/4c01fe0978d957225047ba48d7bdfb99 to your computer and use it in GitHub Desktop.
(() => {
const timerElement = document.getElementById('timer')
const startTime = new Date().getTime()
function pad(num, size) {
let s = "0000" + String(num)
return s.substr(s.length - size)
}
function getTime() {
const now = new Date().getTime()
const distance = now - startTime
return {
hours: Math.floor((distance / (1000 * 60 * 60))),
minutes: Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)),
seconds: Math.floor((distance % (1000 * 60)) / 1000)
}
}
function update() {
timerElement.innerHTML = `${getTime().hours}:${pad(getTime().minutes, 2)}:${pad(getTime().seconds, 2)}`
}
var clocktimer = setInterval(update, 1000)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment