Pause when holding down key

javascript, keydown

Solution

Make an interval that increases the objects, on key down, and stop the interval on keyup. (Save the interval ID somewhere, also make sure to not make the interval twice when it's already there)

Problem

So I draw an object on the screen at objectx, objecty and increment objectx when right arrow is pressed, moving the object to the right. The problem I'm running into is that if I hold the right arrow key down it increments once, pauses, and then increments repeatedly. My question is why does it do this, and how can I make the object move fluidly without that initial pause? ``` $(window).keydown(function(e) { if(e.keyCode == 39) { objectx++; } } ```

Original source

Related problems