RogerE
RogerE

Reputation: 21

button click prevents second sound playing

I have a simple Javascript program that plays a sound when correct button is clicked in response to a question. If the user answers a second question too quickly and clicks a second correct answer the sound doesnt play a second time as first play has not completed. Do I need to disable the answer buttons for the duration of the sound? Any other ways roound? Suggestions please

Scruffy code added 27.9.2022

        function setQuestion(){
            // string question 
            var questionText =" x 3 = ";
            questionText=aTimes[index]+ questionText;
            document.getElementById("Question").innerHTML= questionText;
            aAnswers[0]=(aTimes[index]*3);
            aAnswers[1]=Math.floor((Math.random() *10) *(aTimes[index] + 1));
            aAnswers[2]=Math.floor((Math.random() *10) * (aTimes[index] + 2));
            shuffleArray(aAnswers)
            document.getElementById("Answer1").innerHTML= aAnswers[0];
            document.getElementById("Answer2").innerHTML= aAnswers[1];
            document.getElementById("Answer3").innerHTML= aAnswers[2];
        }
        function answerCheck(poss){
            //*****************************************************************                                                     
            //***   does the button clicked match the correct multiple of 3                                                 
            //*****************************************************************                                                 
           if (poss.innerHTML==(aTimes[index]*3) ) {
              changePicture();
              index=(index+1);
              **playAudio();**
              resetButtons();
              setQuestion() ; 
              if (index==10){
                  alert("done");
              }
            }
            else{
               alert("not a match");
               poss.style.backgroundColor = "grey";    
            }
        }
        function resetButtons(){
             document.getElementById("Answer1").style.backgroundColor = "#008CBA" ;
            document.getElementById("Answer2").style.backgroundColor = "#008CBA";
            document.getElementById("Answer3").style.backgroundColor = "#008CBA";
        }
        **function playAudio()** { 
        //************** Play a reward sound 
            var x = document.getElementById("myAudio"); 
            x.play(); 
        } 
        </script>

Upvotes: 1

Views: 48

Answers (1)

Raphael Serota
Raphael Serota

Reputation: 2197

The HTML audio element probably isn't a great fit for what you want to do here. I'd recommend using a library like Howler.

Upvotes: 1

Related Questions