is there a way to pause an audio after being played with vanilla js?

  javascript

Hi i’m trying to make an element to play/paused a sound when clicked using the audio object methods: audio.play() and audio.pause() but i’m getting the error:

"Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause(). "

Is there an easy way to resolve this?

document.querySelector(".head-container").addEventListener("click", function () {
    var audio = new Audio('./sounds/kick-bass.mp3');
    audio.play();
    if (!audio.paused) {
        audio.pause();
    }
});

Source: Ask Javascript Questions

LEAVE A COMMENT