Skip to content

Instantly share code, notes, and snippets.

@nullenc0de
Created July 4, 2022 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullenc0de/bdfaf431e924d9bdbafdf5747de40541 to your computer and use it in GitHub Desktop.
Save nullenc0de/bdfaf431e924d9bdbafdf5747de40541 to your computer and use it in GitHub Desktop.
<html>
<head>
<script>
document.addEventListener("DOMContentLoaded", function() {
var workouts = [
'Pushups',
'Situps',
'Squats',
'Pullups',
'Lunges',
'Burpees',
'Plank',
'Jumping Jacks',
'Mountain Climbers',
'High Knees',
'Crunches',
'Jump Rope',
'Bicycle Crunches',
'Supermans',
'Jumping Squats'
];
var workout = document.createElement('div');
document.body.appendChild(workout);
var timer = document.createElement('div');
document.body.appendChild(timer);
var startTime = new Date();
function displayWorkout() {
var randomWorkout = workouts[Math.floor(Math.random() * workouts.length)];
workout.innerHTML = randomWorkout;
var randomTime = Math.floor(Math.random() * 60) + 30;
var endTime = new Date(startTime.getTime() + randomTime * 1000);
var interval = setInterval(function() {
var timeLeft = endTime - new Date();
if (timeLeft <= 0) {
clearInterval(interval);
timer.innerHTML = '0:00';
displayWorkout();
} else {
var minutes = Math.floor(timeLeft / 60000);
var seconds = Math.floor((timeLeft % 60000) / 1000);
timer.innerHTML = minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
}
}, 1000);
}
displayWorkout();
workout.style.textAlign = 'center';
workout.style.fontSize = '100px';
timer.style.textAlign = 'center';
timer.style.fontSize = '100px';
timer.addEventListener('DOMSubtreeModified', function() {
if (timer.innerHTML === '0:00') {
startTime = new Date();
}
});
timer.addEventListener('DOMSubtreeModified', function() {
if (timer.innerHTML === '0:01') {
var audio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
audio.play();
document.body.style.backgroundColor = 'red';
setTimeout(function() {
document.body.style.backgroundColor = 'white';
}, 20);
}
});
timer.addEventListener('DOMSubtreeModified', function() {
if (timer.innerHTML === '0:10') {
document.body.style.backgroundColor = 'yellow';
}
});
});
</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment