title: How can I get a (very) short .mp3 audio file to play on every keystroke?
body: <p>I am building a web-app which includes a form and (as a progressive enhancement / UX effect) I would like a keypress to fire a keystroke sound effect.</p>
  
  <p>I have cut the .mp3 sound effect quite short (0.18s).</p>
  
  <p>Nevertheless, there appears to be an audible delay when I test my setup on Firefox on my laptop (and not every keypress fires the sound effect).</p>
  
  <p>On Firefox Mobile on my Android, very few keypresses fire the sound effect - maybe one keypress in every eight.</p>
  
  <p>Am I trying to achieve something which can't be done using present web technology, or am I simply approaching this the wrong way?</p>
  
  <p>Here is my setup:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var myInput = document.getElementsByTagName('input')[0];
  var keypress = document.getElementsByClassName('keypress')[0];
  
  function playSoundEffect() {
      keypress.play();
  }
  
  myInput.addEventListener('keydown', playSoundEffect, false);
  
  /* I've also tried...
  myInput.addEventListener('keyup', playSoundEffect, false);
  myInput.addEventListener('keypress', playSoundEffect, false);
  */</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>input {
  width: 90vw;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input type="text" placeholder="Type to hear the sound effect..." /&gt;
  
  &lt;audio class="keypress"&gt;
  &lt;source src="http://handsoffhri.org/.assets/theme/elements/mp3/keypress.mp3" type="audio/mpeg"&gt;
  &lt;/audio&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <hr>
  
  <h1>Second Attempt:</h1>
  
  <p>Thanks to the helpful comments below from @StackingForHeap and @zero298, I have refactored my setup:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var myInput = document.getElementsByTagName('input')[0];
  
  function playSoundEffect() {
      var jsKeypress = new Audio('http://handsoffhri.org/.assets/theme/elements/mp3/keypress.mp3');
      jsKeypress.play();
  }
  
  myInput.addEventListener('input', playSoundEffect, false);</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>input {
  width: 90vw;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input type="text" placeholder="Type to hear the sound effect..." /&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is definitely an improvement - creating a new <code>Audio</code> object each time allows for each one to start playing independently of any previously created <code>Audio</code> objects.</p>