Reputation: 1606
I am looking for a way to disable all keycodes until the next()
function is complete.
I tried using return false
but it didn't work.
nextDown = {};
$(document).keydown(function(event){
if(event.keyCode == '39'){
if (nextDown['39'] == null) {
next();
nextDown['39'] = true;
}
}
});
$(document).keyup(function(event) {
nextDown[event.keyCode] = null;
});
Is there a way bind false against the keydown
then after the function is complete unbind?
Upvotes: 2
Views: 129
Reputation: 2743
what about declare a variable to do so. Does the following code solve your problem?
nextDown = {};
var _lock = false;
$(document).keydown(function(event){
if(event.keyCode == '39'){
if (!_lock && nextDown['39'] == null) {
_lock = true;
next();
nextDown['39'] = true;
}
}
});
$(document).keyup(function(event) {
nextDown[event.keyCode] = null;
});
function next() {
...
_lock = false;
}
If not, please, provide a jsfiddle example
Upvotes: 2
Reputation: 4505
There is a very easy way to accomplish this. First create a global variable and set it to 0. Then have the cont set to 1 before next() and back to 0 after next() is complete. This will allow you to create if statements where cont == 1
would stop your other functions and cont == 0
would allow them to run.
var cont = 0;
$(document).keydown(function(event){
if(event.keyCode == '39' && cont == 0){
if (nextDown['39'] == null) {
cont = 1;
next();
cont = 0;
nextDown['39'] = true;
}
}
});
$(document).keyup(function(event) {
if (cont == 0) {
nextDown[event.keyCode] = null;
}
});
Upvotes: 0
Reputation: 2596
You have to use a callback to do what you want.
http://api.jquery.com/jquery.callbacks/
Upvotes: 0