Skip to content

Instantly share code, notes, and snippets.

@manggalang
Created July 13, 2019 05:41
Show Gist options
  • Save manggalang/50fbcae63e25317a2cee58dd5ae9bf1d to your computer and use it in GitHub Desktop.
Save manggalang/50fbcae63e25317a2cee58dd5ae9bf1d to your computer and use it in GitHub Desktop.
Random Name Picker with Countdown Timer
<h1 id="headerNames">?</h1>
<div class="button" id="startButton">start</div>
<div class="button" id="stopButton">stop</div>
<div id="timerWrapper">Time left: <span id="timer"></span></div>
<div id="timesUp">
Time's Up!
<a href="https://codepen.io/screener13/pen/PJJLLo">RESET</a>
</div>

Random Name Picker with Countdown Timer

Runs through a list of names at fast speed. Stops on a name when you click the stop button.

A Pen by Michael Oquendo on CodePen.

License.

"use strict";
// Change to false if you don't want a timer
const showTimer = false;
// Set timer countdown time here in minutes : seconds format
const time = 0 + ":" + 19;
// Add list of names here
const namesList = [
'Ricardo',
'Adam',
'Ryan',
'Michelle',
'Mike',
'John'
];
// Default variables
let i = 0;
let x = 0;
let intervalHandle = null;
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
const headerOne = document.getElementById('headerNames');
const timesUp = document.getElementById('timesUp');
const timerWrapper = document.getElementById('timerWrapper');
const timer = document.getElementById('timer');
// Start or stop the name shuffle on button click
startButton.addEventListener('click', function() {
this.style.display = "none";
stopButton.style.display = "block";
intervalHandle = setInterval(function () {
headerNames.textContent = namesList[i++ % namesList.length];
}, 50);
if (showTimer===true) {
timerWrapper.classList.remove('visible');
}
});
stopButton.addEventListener('click', function() {
this.style.display = "none";
startButton.style.display = "block";
clearInterval(intervalHandle);
timer.innerHTML = time;
if (showTimer===true) {
timerWrapper.classList.add('visible');
}
startTimer();
});
// Allow use of spacebar to start/stop name shuffle
document.body.onkeyup = function(e) {
if (e.keyCode == 32) {
if (x%2===0) {
startButton.style.display = "none";
stopButton.style.display = "block";
intervalHandle = setInterval(function () {
headerNames.textContent = namesList[i++ % namesList.length];
}, 50);
if (showTimer===true) {
timerWrapper.classList.remove('visible');
}
} else {
startButton.style.display = "block";
stopButton.style.display = "none";
clearInterval(intervalHandle);
timer.innerHTML = time;
if (showTimer===true) {
timerWrapper.classList.add('visible');
}
startTimer();
}
x++;
}
}
body {
background: #333;
}
h1#headerNames {
margin-top: 10%;
color: #fff;
font-family: Georgia, serif;
font-size: 150px;
text-align: center;
cursor: pointer;
}
.button {
width: 150px;
margin: auto;
padding: 20px;
background: #4db2ec;
border: 3px solid #fff;
border-radius: 10px;
color: #fff;
font-family: Arial, sans-serif;
font-size: 30px;
font-weight: bold;
text-align: center;
text-transform: uppercase;
letter-spacing: 2px;
display: block;
cursor: pointer;
}
#stopButton {
background: #ff0000;
display: none;
}
#timerWrapper {
margin: 50px 0;
color: #fff;
font-family: Arial, sans-serif;
font-size: 50px;
text-align: center;
opacity: 0;
transition: opacity 1s;
}
#timerWrapper.visible {
opacity: 1;
}
#timesUp {
padding-top: 20%;
background-color: red;
color: #fff;
font-family: Arial, sans-serif;
font-size: 100px;
font-weight: bold;
text-transform: uppercase;
text-align: center;
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
display: none;
a {
margin: 100px auto;
font-size: 15px;
position: absolute;
bottom: 50px;
left: 0;
right: 0;
display: none;
}
}
#timesUp.backgroundRed {
background-color: #333;
}
@media only screen and (max-width: 600px) {
h1 {
font-size: 50px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment