My goal is to keep the animation smooth from the very beginning as you keep pressing button. What causes the lag after the first animation? How to get rid of it while allowing only one queue at a time?
function move() {
var queue = jQuery.queue($("#hero")[0], "fx");
if (queue.length < 2) {
$('#hero').animate({
width: '+=30px'
}, {
easing: 'linear',
queue: false,
duration: 200
});
}
}
$(document).keydown(function(e) {
if (e.keyCode == 68) { //D Right
$("#hero").queue(function() {
move();
$.dequeue(this);
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hero" style="width:10px;height:30px;left:0px;background-color:red;">
</div>