I try to make a fade in & out effect for a music application when I click the next/previous button; I have this sleep function:
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
the function when the button is pushed:
function nextSong() {
songIndex++;
if (songIndex > songs.length - 1) {
songIndex = 0;
}
fadeOut(audio);
loadSong(songs[songIndex]);
playSong();
fadeIn(audio);
}
and the fading functions
async function fadeOut(soundtrack){
for (var i = 1.0; i > 0; i-= 0.01)
{
console.log(i);
console.log(soundtrack.volume);
soundtrack.volume = i;
await sleep(2000);
}
}
async function fadeIn(soundtrack){
for (var i = 0; i <= 1; i+= 0.01)
{
console.log(i);
console.log(soundtrack.volume);
soundtrack.volume = i;
await sleep(100);
}
}
The problem is fadeOut doesn't work at all, it goes in the for loop for 1 iteration and then exists. Meanwhile, fadeIn works perfectly. I just can't understand. Btw this is my first javascript hobby project.