The problem of my code is that it doesn’t countdown to “00:00” or it switchs too fast, so you can only see “00:01” and then it changes to new cycle.
First in my function this.changeName()
I go with this code, but it swithes too fast, so you can not see “00:00”:
changeName() {
if (this.state.timerAmount === 0 && this.state.timerName === 'Session') {
this.playSound();
setTimeout(() => {this.setState(state => ({
timerAmount: this.state.breakMinutes * 60,
timerName: 'Break'
})); },2000);
}
else if (this.state.timerAmount === 0 && this.state.timerName === 'Break') {
this.playSound();
this.setState(state => ({
timerAmount: this.state.sessionMinutes * 60,
timerName: 'Session'
})); }
}
Then I’ve tried by using innerHTML/innerText/innerContent
first stay on “00:00” and after that using setTimeout
for this.setState
to change velue for new cycle with delay, but in this case I get empty element for some time and then it is start new cycle:
changeName() {
if (this.state.timerName === 'Session') {
document.getElementById('time-left').innerHTML = "00:00"
this.playSound();
setTimeout(() => {this.setState(state => ({
timerAmount: this.state.breakMinutes * 60,
timerName: 'Break'
})); },2000);
}
else if (this.state.timerName === 'Break') {
document.getElementById('time-left').innerText = "00:00";
this.playSound();
setTimeout(() => {this.setState(state => ({
timerAmount: this.state.sessionMinutes * 60,
timerName: 'Session'
}));} , 2000);
}
But then I found my problem.
In clockInfo()
I have condition if (timer > 0) return "00:00"
. So when my timer showed “00:00”, it start changeName()
function and that is how I got empty element for some time.
I deleted changeName()
and isertes it’s conditions into clockInfo()
like this:
clockInfo() {
if (this.state.timerAmount < 0) {
if (this.state.timerName === 'Session') {
"00:00"
this.playSound();
this.setState(state => ({
timerAmount: this.state.breakMinutes * 60,
timerName: 'Break'
}));
} else if (this.state.timerName === 'Break') {
"00:00"
this.playSound();
this.setState(state => ({
timerAmount: this.state.sessionMinutes * 60,
timerName: 'Session'
}));
}
} else {
let minutes = Math.floor(this.state.timerAmount / 60);
let seconds = this.state.timerAmount - minutes * 60;
seconds = seconds < 10 ? '0' + seconds : seconds;
minutes = minutes < 10 ? '0' + minutes : minutes;
return minutes + ':' + seconds;
}
}
When timer < 0, first it shaw “00:00”, then play sound and then start function, but in this case without any empty element. So now all works very well.