title: How to play sound only once while holding a keyboard button?
body: <p>Recently I have create a simple code using JavaScript. Basically, it a press to play sound program. The sounds are 3-4 seconds long. Everything worked fine until I found out that if I hold down a key the sound would play itself repeatedly. They didn't finished their length, but keep restart to play at the beginning. All I want is just that it play to the end and stop, and will play again if the key is up and press again. </p>
  
  <p>This is my code : </p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;audio id="fart" source src="" type="audio/mpeg"&gt;&lt;/audio&gt;
      &lt;script src="http://code.jquery.com/jquery-1.11.2.min.js"&gt;&lt;/script&gt;
      &lt;script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          var player = document.getElementById("fart");
          //var keyIsDown = true;
  
          var getCommand = function (pressedChar) {
              var command = null;
              if (keyIsDown) {
                  switch (pressedChar) {
                      case 'a':
                          //alert("I'm A.");
                          player.setAttribute("src", "1fart-07 C7.mp3");
                          player.play();
                          //keyIsDown = false;
                          break;
                      case 'b':
                          player.setAttribute("src", "2fart-05 D7.mp3");
                          player.play();
                          break;
                      case 'c':
                          player.setAttribute("src", "3fart-01 E7.mp3");
                          player.play();
                          break;
                      case 'd':
                          player.setAttribute("src", "4fart-03 F7.mp3");
                          player.play();
                          break;
                      case 'e':
                          player.setAttribute("src", "5fart Na G7.mp3");
                          player.play();
                          break;
                      case 'f':
                          player.setAttribute("src", "6fart Na A7.mp3");
                          player.play();
                          break;
                      case 'g':
                          player.setAttribute("src", "7fart Na B7.mp3");
                          player.play();
                          break;
                      case 'h':
                          player.setAttribute("src", "fart-07 C8.mp3");
                          player.play();
                          break;
                      default:
                          alert("OUT");
                          break;
                  }
                  return command;
              } else {
                  command = null;
              }
          };
          /////
          /*$(document).keyup(function(e){
           keyIsDown = true;
           });*/
  
          //////
          $(document).keypress(function (e) {
              var code = e.keyCode || e.which;
              var pressedChar = String.fromCharCode(code).toLowerCase();
              var command = getCommand(pressedChar);
              keyIsDown = true;
          });
  
          $(document).ready(function () {
              console.log("ready!");
          });
          ///////
      &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I've googled and found <a href="https://stackoverflow.com/questions/14301728/how-do-you-circumvent-repeated-keydown-messages-when-you-want-to-use-them-with-j">this forum</a> so I tried to adapt it to mine, but still didn't work out. Anyone has any suggestion? I also found a way in <a href="https://stackoverflow.com/questions/9335577/html5-audio-sound-only-plays-once">Jquery</a>, but didn't work. I feel like now I'm getting close to the answer, but may be I code something wrong. Please help me. Thanks.</p>