简体   繁体   中英

Javascript switch cases 1, case 2 and case 1 and 2

This is my code:

$(document).keydown(function(e){
    switch(e.keyCode){
        case 39:
            function1();
            break;          
        case 32:
            function2();
            break;
        case 39:
        case 32:
            function3();
            break;
    }
});

It works fine except for case 39: case 32:. It only works when case 39 and 32 are met at the same time. If case 39 is met first and then case 32 is met while case 39 is still met, only case 32 is run. What I'm trying to do is to run function3() when case 39: case 32: is met but not only when both are met at the same time. Is there a workaround for this?

The switch will simply match whichever case statement it encounters first.

In your code, function3 can never execute, because when e.keyCode is 39, it matches the first statement, and when it's 32, it matches the second. It will never reach the third section.

If you want function3 to run when either condition is true:

switch(e.keyCode){
    case 39:
        function1();
        function3();
        break;          
    case 32:
        function2();
        function3();
        break;
}

If you want function3 to always run, just place it afterwards:

switch(e.keyCode){
    case 39:
        function1();
        break;          
    case 32:
        function2();
        break;
}
function3();

Also, the idea that e.keyCode could be two different numbers simultaneously doesn't make any sense. It's just a number variable. It can only hold a single number at a time.

In comments, you said you want to run code when two different keys are pressed one after another. Well, then that's two different firings of this event. You'll have to track some state.

var key39Pressed = false;
var key32Pressed = false;
$(document).keydown(function(e){
     switch(e.keyCode){
        case 39:
            if (key32Pressed)
                function3();
            else
                function1();
            key39Pressed = true;
            key32Pressed = false;
            break;          
        case 32:
            if (key39Pressed)
                function3();
            else
                function2();
            key32Pressed = true;
            key39Pressed = false;
            break;
        default:
            key32Pressed = false;
            key39Pressed = false;
    }
}

I'd use an if statement for this with some additional state tracking.

var pressedKeys = {};

$(document).keydown(function(e) {
  pressedKeys[e.which] = true; //setting it up like this so I can use the "in" operator

  if(39 in pressedKeys) {
    if(32 in pressedKeys) {
      //both 39 and 32 are pressed
      function3();
    } else {
      //only 39 is pressed
      function1();
    }
  } else if(32 in pressedKeys) {
    //only 32 is pressed
    function2();
  }
});

$(document).keyup(function(e) {
  delete pressedKeys[e.which];
});

You may also ...

var t;    
switch(e.keyCode){
        case 39:
          function1();       
          t=1;
          setTimeout(function(){t=0},1000); 
        case 32:
            function2();
           if(t) { 
           function3();}
            break;
    }

So if you press key before 1 second, function3 will call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM