Q:

jQuery - avoid listening to arrow keys while function is executing

SCENARIO
I have developed a function with jQuery which listens to the user's keyboard (based on the keydown() method).  
I analyze the user's input with evt.keyCode, and if the down or up arrow keys are pressed, then an animated smooth scroll is executed. 
PROBLEM
The animation lasts 1 second.  
I don't want the function to listen to any input while the execution of the animations lasts.  
That is, if the user presses several times the down and up arrow keys in 1 second, only the first one should be taken into account, and until the execution time (1 second) is over, the rest of the keystrokes should be discarded.  
How can I accomplish this? 
JS CODE
$(function () { 

  $(document).keydown(function (evt) {

    if (evt.keyCode == 40) {

                $('html,body').animate({scrollTop: 50}, 1000);

    } else if (evt.keyCode == 38) {

                $('html,body').animate({scrollTop: 0}, 1000);

    }

    return false;

  });

});

A:

Set a flag when a key is pressed, reset the flag at the end of the animation:
var isAnimating = false;
$(document).keydown(function (evt) {
    if (isAnimating == false) {
        isAnimating = true;
    } else {
        return;
    }
    if (evt.keyCode == 40) {

            $('html,body').animate({scrollTop: 50}, 1000, function() {
                isAnimating = false;
            });

    } else if (evt.keyCode == 38) {

            $('html,body').animate({scrollTop: 0}, 1000, function() {
                isAnimating = false;
            });

    }

    return false;
});

A:

Check to see if you're already animating using .is(":animated")
$(function () { 
  $(document).keydown(function (evt) {
    if ($('html,body').is(":animated")){
        return false;
    }
    if (evt.keyCode == 40) {
                $('html,body').animate({scrollTop: 50}, 1000);
    } else if (evt.keyCode == 38) {
                $('html,body').animate({scrollTop: 0}, 1000);
    }
    return false;
  });
});

