I've got this code which work perfectly if I want to move one block, but when I will keep the key pressed, animation will work but with those pauses. I figured out that multiply speed and distance will work, but I have no idea how to add this "live" to animation. Can you give me some advice?
var Xpos = 0;
var Ypos = 0;
var spd = 250;
var dstnc = 100;
$(document).keydown(function(e) {
if (e.keyCode == 87) {
$('#movThs').animate({
top: -dstnc + Ypos + 'px'
}, spd);
Ypos = Ypos - dstnc;
}
if (e.keyCode == 68) {
$('#movThs').animate({
left: dstnc + Xpos + 'px'
}, spd);
Xpos = Xpos + dstnc;
}
if (e.keyCode == 83) {
$('#movThs').animate({
top: dstnc + Ypos + 'px'
}, spd);
Ypos = Ypos + dstnc;
}
if (e.keyCode == 65) {
$('#movThs').animate({
left: -dstnc + Xpos + 'px'
}, spd);
Xpos = Xpos - dstnc;
}
});
#movThs {
background-color: #ff0000;
width: 100px;
height: 100px;
position: relative;
border-radius: 90%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="movThs"></div>