So, when timer goes to 00:00 the state of the display changes and the sound plays at that moment. How can I make the sound play at exactly 00:00 and then change the display? Thanks in advance! Here’s my Pen and the relevant code:
let timerID = null;
function toggleClock() {
if (!this.props.parentState.isPaused === false) {
clearInterval(timerID);
timerID = setInterval(() => this.getTime(), 1000);
} else {
clearInterval(timerID);
}
}
class Clock extends React.Component {
constructor(props) {
super(props);
this.getTime = this.getTime.bind(this);
this.playBeep = this.playBeep.bind(this);
resetClock = resetClock.bind(this);
resetSound = resetSound.bind(this);
toggleClock = toggleClock.bind(this);
this.state = {
minutes: this.props.minutes,
seconds: "00"
};
}
playBeep() {
this.refs.beepSound.play();
}
getTime() {
if (!this.props.isPaused) {
if (this.state.minutes === "00" && this.state.seconds === "00") {
this.playBeep();
switch (this.props.parentState.type) {
case "Session":
let newBreakLen =
this.props.parentState.breakLength >= 10
? this.props.parentState.breakLength
: "0" + this.props.parentState.breakLength;
this.setState({
minutes: newBreakLen,
seconds: "00"
});
this.props.handler({
type: "Break",
len: newBreakLen,
sessionLength: this.props.parentState.sessionLength,
breakLength: this.props.parentState.breakLength,
isPaused: false
});
break;
case "Break":
let newSessionLen =
this.props.parentState.sessionLength >= 10
? this.props.parentState.sessionLength
: "0" + this.props.parentState.sessionLength;
this.setState({
minutes: newSessionLen,
seconds: "00"
});
this.props.handler({
type: "Session",
len: newSessionLen,
sessionLength: this.props.parentState.sessionLength,
breakLength: this.props.parentState.breakLength,
isPaused: false
});
break;
}
} else if (this.state.seconds === "00") {
let newMin = Number(this.state.minutes) - 1;
newMin = newMin >= 10 ? newMin.toString() : "0" + newMin.toString();
this.setState({
minutes: newMin,
seconds: "59"
});
} else {
let newSec = Number(this.state.seconds) - 1;
newSec = newSec >= 10 ? newSec.toString() : "0" + newSec.toString();
this.setState({
minutes: this.state.minutes,
seconds: newSec
});
}
}
}
render() {
return (
<div>
<h1 id={this.props.id}>
{this.state.minutes}:{this.state.seconds}
</h1>
<audio
id="beep"
ref="beepSound"
src="https://onlineclock.net/audio/options/default.mp3"
>
<source
src="https://onlineclock.net/audio/options/default.mp3"
type="audio/mpeg"
/>
</audio>
</div>
);
}
}
