I have a function in jQuery which is triggered whenever I press any of the arrow keys. The following is the code for the 'up' arrow case, and the other three cases look very similar and are contained within the checkKey function.
document.onkeydown = checkKey;
function checkKey(e) {
var divs = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen"];
e = e || window.event;
if (e.keyCode == '38') {
for (i = 0; i < divs.length; i++){
var tmp = document.getElementById(divs[i]);
var coord = $(tmp).position();
if ((Math.abs(embargo_top - coord.top) < 115) && (Math.abs(embargo_left - coord.left) < 15)){
if(embargo_top < coord.top){
embargo_top = coord.top;
embargo_left = coord.left;
$(tmp).animate({
'top': (parseInt($(tmp).css('top'), 10) - 100) + "px"
}, 500);
break;
}
}
}
checkIfWinner();
}
How do I handle these events so that if I button mash a bunch of arrow keys, each execution of the function only occurs after the previous function call has COMPLETELY finished?