Search code examples
javascriptaudioalertsimultaneous-calls

Play sound and show alert at the same time javascript


Here is my javascript code where i'm trying to play sound then show alert but i always get the alert first and when i click ok the sound is played.

 <script>
            function play() {
                var audio = document.getElementById("audio");
                audio.play();
                alert('Extraction is finished');
            }
  </script>

I'm calling this function in oncomplete command button. I need to play the sound first then show the alert or both at the same time. How can i make this work???


Solution

  • You could set a timeout (change the milisecondsif you want: 1000 )

    function play() {
                var audio = document.getElementById("audio");
                audio.play();
                setTimeout(function(){alert("Extraction is finished");},1000);
            }