Repeats keydown event with Jquery, when using a quick key [closed]

0

Best regards. It turns out that I use the following quick key functionality:

    $(document).ready(function () {
       
        $(document).on('keydown', 'body', function (event) {

         //codigo bla bla
         alert("ejemplo");

      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

was key (any key for all cases), it shows me three times the alert.

It's as if something is accumulating key-key events to repeat itself.

But the strangest thing, when doing the same test action, does not always happen, other times it does not give this problem.

Should I clean the keydown or declare it in another way? Any ideas?

    
asked by Danilo 30.05.2017 в 04:19
source

1 answer

1

I've been testing your snippet and it only shows alert once.

In what context do you use this function? Is it possible that the script is loading more than once? If so, you would associate more than one event with document . The behavior would be similar to this:

$(document).ready(function () {
   
  $(document).on('keydown', 'body', function (event) {

    //codigo bla bla
    alert("ejemplo");

  });
  
});

$(document).ready(function () {
   
  $(document).on('keydown', 'body', function (event) {

    //codigo bla bla
    alert("ejemplo");

  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 30.05.2017 в 10:22