Right now, I’ve got it so that you can press the left arrow key to perform an event (event 1). If you press the right arrow key after that event, it will perform another event (event 2).
How can I restrict it so that if the left arrow key is pressed twice, it won’t repeat event 1?
Right now, I’ve got:
$(document).keydown(function(f){
if (f.keyCode == 39) {
// do event 1 here with the left arrow key
}
$(this).keydown(function(f){
if (f.keyCode == 39) {
// do event 2 with the right arrow key
window.location.href = "index.php";
}
});
Do I need to set a variable set to false and then true after event 1 has completed?
Use
$(this).unbind('keydown')to remove the handler after your code has been executed.PS: Usually you’d use the
.one()method for a one-time event, but since the event also fires in cases where you don’t want to unbind it (a different key has been pressed), you need to do the unbinding manually.