I am currently working on a breathing exercise code that involves counting in the following manner:
8-second inhale
8 second "hold inhale"
12-second "exhale"
8-second "hold exhale"
Everything is functioning correctly except for the counter, which continues to increment without resetting back to 1. Ideally, it should count from 1 to 8, then reset to 1 and count to 8 again, followed by 1 to 12, and so forth.
// Function to initiate the timer
function startTimer() {
var timer = document.getElementById('timer');
var label = document.getElementById('label');
var count = 1;
// Timer interval function
var interval = setInterval(function() {
// Updating the timer display and label
timer.textContent = count;
if (count <= 8) {
label.textContent = 'Inhale';
} else if (count <= 16) {
label.textContent = 'Pause Inhale';
} else if (count <= 28) {
label.textContent = 'Exhale';
} else if (count <= 36) {
label.textContent = 'Pause Exhale';
}
// Increase the count
count++;
// Stopping the timer after reaching the pattern of 8, 8, 12, 8
if (count === 45) {
clearInterval(interval);
startTimer();
}
}, 1000); // 1000 milliseconds = 1 second
}
// Initializing the timer
startTimer();
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
font-size: 24px;
font-weight: bold;
}
p {
font-size: 18px;
}
<h1>Special Timer</h1>
<p id="timer"></p>
<p id="label"></p>