dimanche 25 octobre 2020

I've tried unsuccessfully to use Math.random to play song files on click button without repeating

<a href="#" id="soundbutton">Press</a>
     <script>
      
      var lastNum; 
         
     function mySounds() {

var x = Math.floor((Math.random() * 5) + 1); if(x === lastNum){sound();}

var sound = new Audio();     
  

switch (x) { case 1: sound.src = "CLA.WAV"; break; case 2: sound.src = "TBN.WAV"; break; case 3: sound.src = "TRI.WAV"; break; case 4: sound.src = "DB.WAV"; break; case 5: sound.src = "REC.WAV"; break; } sound.play();

   lastNum = x;    

}

document.getElementById('soundbutton').addEventListener('click', mySounds);

     </script>
     



Aucun commentaire:

Enregistrer un commentaire