-
-
Save michaelficarra/e1b5806f7bbf1c825695 to your computer and use it in GitHub Desktop.
countdown timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Countdown Timer</title> | |
<style> | |
body, html { | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
height: 100%; | |
margin: 0; | |
padding: 0; | |
} | |
@keyframes blinkingRed { | |
from { background-color: red; } | |
to { background-color: white; } | |
} | |
body.good { | |
background-color: lightgreen; | |
} | |
body.notSoGood { | |
background-color: yellow; | |
} | |
body.bad { | |
animation: blinkingRed ease-in 1s infinite alternate; | |
} | |
body.dead { | |
animation: blinkingRed 0.25s infinite alternate; | |
} | |
time { | |
margin: auto auto; | |
font-family: sans-serif; | |
font-size: 15em; | |
} | |
</style> | |
</head> | |
<body> | |
<time></time> | |
<script> | |
var timeElement = document.getElementsByTagName("time")[0]; | |
var startingTime = parseFloat(document.location.hash.slice(1)) || 20 * 60; // seconds | |
var timeRemaining = startingTime; | |
function pad2(n) { | |
var s = "" + n; | |
return "00".slice(s.length) + s; | |
} | |
function formatTime(time) { | |
var h = Math.floor(time / 3600); | |
var m = Math.floor((time % 3600) / 60); | |
var s = time % 60; | |
return pad2(h) + ":" + pad2(m) + ":" + pad2(s); | |
} | |
timeElement.innerText = formatTime(timeRemaining); | |
var timeoutId = setInterval(function(){ | |
--timeRemaining; | |
timeElement.innerText = formatTime(timeRemaining); | |
if (timeRemaining <= 0) { | |
clearTimeout(timeoutId); | |
document.body.className = "dead"; | |
} else if (timeRemaining <= startingTime / 20) { | |
document.body.className = "bad"; | |
} else if (timeRemaining <= startingTime / 5) { | |
document.body.className = "notSoGood"; | |
} else { | |
document.body.className = "good"; | |
} | |
}, 1000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment