wait till function execute completely to execute the other - javascript

I have this jquery code to handle some cases if user clicked right or left button but the navigate became a mess when the user click both buttons together
$(document).ready(function(){
$(document).keydown(function(e){
if (e.keyCode == 37) {
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39){
setTimeout(RightKeyPressed(),5000);
}
});
});
I want to simply block any call till the first or second function completely executed

Use a flag to block the unnecessary function calls like this,
$(document).ready(function(){
var xKeyPressed = false;
$(document).keydown(function(e){
if (e.keyCode == 37 && !xKeyPressed) {
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39 !xKeyPressed){
setTimeout(RightKeyPressed(),5000);
}
});
function LeftKeyPressed()
{
xKeyPressed = true;
//Your code
xKeyPressed = false;
}
function RightKeyPressed(){
xKeyPressed = true;
//Your code
xKeyPressed = false;
}
});

Set a simple check if your code is running LeftKeyPressed or RightKeyPressed like this:
var inFunction = false;
$(document).ready(function(){
$(document).keydown(function(e){
if (e.keyCode == 37 && !inFunction) {
inFunction = true;
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39 && !inFunction){
inFunction = true;
setTimeout(RightKeyPressed(),5000);
}
});
});
//Add this in your functions, both Left and Right variants,
function LeftKeyPressed(){
//Do your normal stuff here
inFunction = false;
}
There is other ways of doing this as well, but this is in my opinion a clear and concise way of doing it. You can potentially get race conditions with this approach.

$(document).ready(function(){
var navt;
$(document).keydown(function(e){
if(navt != null) return;
if (e.keyCode == 37) {
navt = setTimeout(function(){ LeftKeyPressed(); navt = null; },5000);
}
else if(e.keyCode == 39){
navt = setTimeout(function(){ RightKeyPressed(); navt = null; },5000);
}
});
});

Related

e is undefined, correct syntax for a recursive handler function

How to make a correct recursive call? In this code the console says me "e is undefined".
It's a event listener for a button ul in a unordered list.
This is my code:
$("button#ul").on("click", function(event){
var button = $(this);
$(button).toggleClass("active");
if ($(button).hasClass('active')){
//event listener
$(document).on("keydown", **checkExit** = function(e) {
if (exit(e) == true){
$(button).removeClass('active');
$(this).off("keydown");
}
**checkExit();**
});
function exit(event){
// alert("prevKeyCode: "+prevKeyCode);
var code = event.keyCode || event.which;
var doubleEnter = (code == 13 && prevKeyCode == code);
if (event.type == "click"){
return true;
}
else if (event.type == 'keydown'){
if (code == 27 || doubleEnter) {
prevKeyCode = null;
return true;
}
else if (code == 13) {
prevKeyCode = 13;
}
else {
prevKeyCode = code;
}
return false;
}
}
I think I should define outside the function checkExit...
You need to pass the event as a parameter when calling checkExit again:
$(document).on("keydown", checkExit = function(e) {
if (exit(e) == true) {
$('button').removeClass('active');
$(this).off("keydown"); //spegne il gestore corrente
}
checkExit(e);
});

Key down and up events doesn't work in Javascript

document.addEventListener("keydown", keyD);
document.addEventListener("keyup", keyU);
function keyD(e){
if(e.keycode == 38){
plat1UpP = true;
}else if(e.keycode == 40){
plat1DownP = true;
}
}
function keyU(e){
if(e.keycode == 38){
plat1UpP = false;
}else if(e.keycode == 40){
plat1DownP = false;
}
}
I am trying to make a pong game in html5, javascript and css with canvas, but the events for keydown and keyup don't work.
you must change keycode to keyCode.
consider some browsers doesn't support keyCode and you must use
which.
Change your code to this, it will work for all of them.
document.addEventListener("keydown", keyD);
document.addEventListener("keyup", keyU);
function keyD(e){
var key = e.keyCode || e.which;
if(key == 38){
plat1UpP = true;
}else if(key == 40){
plat1DownP = true;
}
}
function keyU(e) {
var key = e.keyCode || e.which;
if(key ){
plat1UpP = false;
}else if(key){
plat1DownP = false;
}
}
You have syntax error, you new to use e.keyCode insted of e.keycode.

Catch refresh and change page to index.html

i have a question. I want to detect, if a user has pressed F5 or the refresh button. If so, i want to change the page to index.html. Here is my code:
$(window).on('beforeunload',function(){
$.mobile.changePage($(document.location.href="/index.html"),{
transition:"slide",
changeHash:false
});
});
But it's not working. Has anybody a solution, how i can achieve this?
Thanks.
Just get the key value:
$(document).keypress(function(e) {
if(e.which == 116) {
window.location.href = '/index.html';
}
});
Key Reference
Alternative method:
document.onkeydown = fkey;
document.onkeypress = fkey
document.onkeyup = fkey;
var wasPressed = false;
function fkey(e){
e = e || window.event;
if( wasPressed ) return;
if (e.keyCode == 116) {
window.location.href = '/index.html';
wasPressed = true;
}else {
alert("Window closed");
}
}

keydown event not fired, why?

In JavaScript, I hold two keys down, and keydown is fired perfectly. When I release one of the keys, keyupis fired. So far so good. But I am still holding one key down, so why arent keydown fired? I need this to happen in my game. Am I doing something wrong? Is this the expected response? Is there a work around or something?
window.addEventListener("keydown",
function (e) {
console.log('down');
}, false);
window.addEventListener('keyup',
function (e) {
console.log('up');
}, false);
Looks to me like you're trying to do something like this:
var down = false;
var up = false;
document.body.addEventListener('keydown', function (e) {
if(e.which === 40) {
down = true;
}
if(e.which === 38) {
up = true;
}
});
document.body.addEventListener('keyup', function (e) {
if(e.which === 40) {
down = false;
}
if(e.which === 38) {
up = false;
}
// logic here for one but not the other
if(down && !up) {
alert('down but not up!');
}
});

How to limit Tab listener to callback only if the textbox has change

I'm trying to implement a Tab key listener for a textbox.
$('#mytextbox').live('keydown', function (e) {
if (e.keyCode == 9 || e.which == 9) {
// TO DO SOMETHING
}
});
However, for some reason I need to limit the tab listener's callback to invoke only when the textbox has changed. Is there anyway to do this?
You might be able to check the value of the input field to make sure it's different from it's original value?
E.g.
$('#mytextbox').live('keydown', function (e) {
if ((e.keyCode == 9 || e.which == 9) && ($('#TextBox').val() != 'Starting Value')) {
// TO DO SOMETHING
}
});
You can do it just like:
var data = "";
$('#mytextbox').live('keydown', function (e){
if(e.which == 9 || e.keyCode == 9){
if($(this).val() != data){
alert('changed!');
data = $(this).val();
}
}
});
http://jsfiddle.net/DDCZS/1/
Or without storing / knowing value of that textbox:
var changed = false;
$('#mytextbox').on('keydown', function (e) {
if (e.which == 9 && changed) {
e.preventDefault();
// TO DO SOMETHING
alert("works");
changed = false;
} else {
changed = true;
}
});
http://jsfiddle.net/9a37b/

Categories