title: Javascript - Check if key was pressed twice within 5 secs
body: <p>I want to check if Enter key was pressed twice within 5 secs and perform some action.
  How can I check if the key was pressed once or twice within a given time and perform different actions.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>&lt;h1 id="log"&gt;0&lt;/h1&gt;
  &lt;br/&gt;
  &lt;span id="enteredTime"&gt;0&lt;/span&gt;
  
  &lt;script&gt;
  $(document).keypress(function(e) {
      if(e.which == 13){
          var element = $("#log");
          var timeDifference = 0;
  
          //Log the timestamp after pressing Enter
          $("#enteredTime").text(new Date().getTime());
  
          //Check if enter was pressed earlier
          if ($("#enteredTime").text() !== "0") {
              var now = new Date().getTime();
              var previous = $("#enteredTime").text();
              difference = now - previous;
          }
          //Check if enter was pressed only once within 5 secs or more
          if(){
              $("#log").text("Once");
              $("#enteredTime").text("0");
          //Check if enter was pressed twice in 5 secs
          }else{
               $("#log").text("Twice in less than 5 secs");
               $("#enteredTime").text("0");
          }
  
      }
  
  });
  &lt;/script&gt;
  </code></pre>
  
  <p><a href="http://jsfiddle.net/Rjr4g/" rel="nofollow">http://jsfiddle.net/Rjr4g/</a></p>
  
  <p>Thanks!</p>