lisarko8077
lisarko8077

Reputation: 307

Javascript delay on audio play on click

I want to build a Simon Game. I want to play a sound on click but the sound has a noticeable delay(4/5 sec). How can i prevent it and play the sound exactly on click ?

Here the code:

var blue = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
$(".but").on('click', function(){
    blue.play();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="but">Play</button>

Upvotes: 1

Views: 1191

Answers (1)

Dan Kreiger
Dan Kreiger

Reputation: 5516

I don't hear a delay. Try the snippet below or this CodePen Demo:

$(document).ready(function() {
  var blue = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
  $(".but").on("click", function() {
    blue.play();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button class="but">click for sound</button>

Upvotes: 1

Related Questions