Reputation: 21
I´m working on another assignment for my Frontend course and I have a function which play a sound if the user enable the function from settings in on the website. When the user first open the website it stores isMusicEnable = false to localStorage and when the user presses enable music in settings it changes to true. I have gotten that to work. When the user enable music it starts to play but when the user press disable music it keeps playing. I have checked and the isMusicEnable changes to false in localStorage.
var audio = new Audio('/sound/menyMusic.mp3');
// audio.volume = this.getAudioVolume();
if (this.isMusicOn() == true){
audio.loop = true;
audio.play();
}
else if(this.isMusicOn() == false){
audio.pause();
audio.currentTime = 0;
}
},
let musicEnable = this.getItem('isMusicEnable');
if (musicEnable == 'true'){
return true;
}
return false;
},
I dont understand why it dosen´t work.
Upvotes: 0
Views: 195
Reputation: 731
I suspect that your this.isMusicOn()
function always returns true
.
Regardless of the answer, your code is redundant. Any value is always interpreted either as true or as false. In addition, the argument of "if" is cast to a Boolean value. So, comparison with "true" does not make sense. Therefore, the code can be simplified:
// audio.volume = this.getAudioVolume();
if (this.isMusicOn()) {
audio.loop = true;
audio.play();
}
else {
audio.pause();
audio.currentTime = 0;
}
Upvotes: 1