Skip to content

Instantly share code, notes, and snippets.

@jim-clark
Last active October 17, 2020 06:21
Show Gist options
  • Save jim-clark/b9e1ff8fc174c8b961f5 to your computer and use it in GitHub Desktop.
Save jim-clark/b9e1ff8fc174c8b961f5 to your computer and use it in GitHub Desktop.
WDI Lesson Timer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Countdown</title>
<style type="text/css">
html, body {
font-family: helvetica;
font-weight: lighter;
margin: 0;
height: 100%;
}
#buttons {
text-align: center;
position: relative;
top: 50%;
transform: translateY(-50%);
}
button {
font-size: 20px;
padding: 20px;
}
input {
font-size: 20px;
padding: 20px;
width: 60px;
margin-left: 20px;
}
#time {
font-size: 160px;
text-align: center;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.inside-minute {
color: red;
}
</style>
</head>
<body>
<div id="buttons">
minutes:<br>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>10</button>
<input id="custom" type="number">
<button>custom</button>
</div>
<div id="time"></div>
<script>
(function() {
var secs, timer, text,
el = document.getElementById('time');
document.getElementById('buttons').addEventListener('click', function(evt) {
if (evt.srcElement === document.getElementById('custom')) return;
this.style.display = 'none';
secs = (evt.srcElement.innerHTML === 'custom') ?
parseInt(document.getElementById('custom').value) * 60 :
parseInt(evt.srcElement.innerHTML) * 60;
updateDisplay();
timer = setInterval(function() {
secs--;
updateDisplay();
}, 1000);
});
function updateDisplay() {
if (secs === 0) {
clearInterval(timer);
text = "BAM!";
} else {
text = (secs % 60) < 10 ? '0' + secs % 60 : '' + secs % 60;
text = Math.floor(secs / 60).toString() + ':' + text;
}
if (secs === 60) el.className = 'inside-minute';
el.innerHTML = text;
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment