JavaScript if a key is pressed for 500 milliseconds trigger a function [duplicate]

This question already has answers here:
Answer:1

I'm not sure if this is best approach, but this would be solve your problem

var pressedAt = 0
var timeStep = 500 // 500 ms

function checkKey(e) {
 if(pressedAt == 0) {
  pressedAt = Date.now() 
 }
 if (Date.now() - pressedAt >= timeStep) {
  console.log("Key Pressed");
  pressedAt = 0
 }
}

document.addEventListener("keydown", checkKey);
document.addEventListener("keyup", function(){
  pressedAt = 0
  console.clear()
});
Answer:2

I'm not familiar with JQuery, but...

Here's a solution with a slightly different result than that of the other answers.

The 500ms timer is not dependent on the key presses.

let isAHeld = false;
$(document).keydown((event) => {
  if (event.key === 'a') {
    isAHeld = true;
  }
});
$(document).keyup((event) => {
  if (event.key === 'a') {
    isAHeld = false;
  }
});
$(document).ready(() => {
  setInterval(() => {
    const result = $("#result")[0];
    if (result && isAHeld) {
      result.innerHTML += " A!";
    }
  }, 500);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Click here to give focus, then press and hold "a".</div>
<div id="result">Output:</div>
Answer:3

I'm trying to get text to show up in an SVG element I created. I created a textNode and then appended that to a SVG text element, but it doesn't seem to be showing up. The SVG element is showing up ...

I'm trying to get text to show up in an SVG element I created. I created a textNode and then appended that to a SVG text element, but it doesn't seem to be showing up. The SVG element is showing up ...

I have a scrollable list and I would like to know when I've reached the end. Example: HTML: <ol> <li>a</li> <li>b</li> .... </ol> CSS: ol { ...

I have a scrollable list and I would like to know when I've reached the end. Example: HTML: <ol> <li>a</li> <li>b</li> .... </ol> CSS: ol { ...

  1. find your code iphone
  2. find your code name
  3. find your code
  4. find your code move data from android
  5. find your code ios
  6. find your code iphone setup
  7. find embed code youtube
  8. bible code find your name
  9. how to find your code generator on facebook
  10. find your code move to ios
  11. find tv code for youtube
  12. find out what tax code your on
  13. how to find your code on beem it
  14. how to find your code in bts world
  15. how to find your code on discord
  16. how to find your code on mario kart
  17. how to find your code on messenger
  18. how to find your code on facebook
  19. how to find your zip code
  20. how to find your code in steam

I have a little script at the bottom of the webpage with the following: <script language="Javascript"> var d = document.lastModified; var n = d.toLocaleString(); document.write("...

I have a little script at the bottom of the webpage with the following: <script language="Javascript"> var d = document.lastModified; var n = d.toLocaleString(); document.write("...

I'm new to PHP and very lost. I'm working with HTML5, CSS3, jQuery and Bootstrap 4. I have a total of 4 HTML pages on my website. In the first page there are 4 squares with text (let's say A, B, C ...

I'm new to PHP and very lost. I'm working with HTML5, CSS3, jQuery and Bootstrap 4. I have a total of 4 HTML pages on my website. In the first page there are 4 squares with text (let's say A, B, C ...

  1. passing data among methods in java