JSFiddle - React, Tailwind, and code Playground

HTML

<p>Click me to get focus and then press a key for 500ms or longer.</p>

JavaScript

var timer1 = null;
var time = 500;


$(document).on('keydown', function() {

		//console.log('keydown has been called');

    // init once, because for some keys (e.g. space, letters, etc.) 
    // this handler will be called consistently while you press the button.
    // some other buttons like "strg", "cmd", etc. will be called once only
		if(timer1 === null) {

      // now from exec each 500ms
      timer1 = setInterval( function() {

        $('body').append( '<div>I will be called each 500ms</div>' )
      }, time);  		
    }
});

$(document).on('keyup', function() {

	clearInterval(timer1);
  
	timer1 = null;
});