Skip to content Skip to sidebar Skip to footer

Javascript Audio Plays Twice

I'm building a simple HTML5/Javascript game and wish to play a sound file when something is clicked. I have the following: function tileClickListener(e) { e.preventDefault();

Solution 1:

This naive implementation seems to play only once for me.

Array.from(document.querySelectorAll(".tile")).forEach(function(el){
  el.addEventListener("click", tileClickListener);
});

functiontileClickListener(e) {
    e.preventDefault();
    console.log('tile clicked');
    var audio = newAudio('http://www.soundjay.com/button/beep-01a.wav');
    audio.play();
}
.tile{
  height: 100px;
  width: 100px;
  background-color: aliceblue;
  border: solid 1px;
  text-align: center;
  line-height: 100px;
}
<divclass="tile">click</div>

Post a Comment for "Javascript Audio Plays Twice"