Play audio when javascript alert pops up and stop playing when the alert closes

I’m trying to play an audio when a javascript alert pops up and stop playing the audio when the user closes the alert. But the audio is not stopping when the user closes the alert.And the alert keeps appearing again and again even after I close it.
Please suggest a solution or a workaround.
Thanks!

const audio = new Audio("sounds/Sparkle - Your Name.mp3");
audio.play();
audio.loop = false;
if(!alert("Time for some anime")){
           audio.pause();
}

47 thoughts on “Play audio when javascript alert pops up and stop playing when the alert closes”

Leave a Comment