Double event in jQuery click

1

I'm having trouble with jQuery events on time. I've already used all best practices to stop the click event. Here are some examples:

.off()
e.preventDefault();
e.stopPropagation();
unbid('click');

Always when there is a click I do this function

function LoadingTela(botao,tipo) {
     if (tipo == true) {
       $(botao).prop("disabled", true); 
       $('.mask-loading').show(); 
     }else { 
       $(botao).prop("disabled", false); 
       $('.mask-loading').hide(); 
     } 
 }

Has anyone come up with any solution or new event control?

    
asked by anonymous 06.06.2018 / 13:37

1 answer

0

You can try with the one function of jQuery. It is similar to the on function, with the difference that the event is removed from the element only its first execution, ie for the example below the function in the click event will be executed only once.

$('#btn').one('click', event => {
  console.log('Botão pressionado');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="btn">Pressione-me!</button>

If it is possible for the event to be triggered multiple times on the same page and the intention is to only protect from multiple simultaneous clicks , you can disable the button (or element that triggers the event) while processing the application:

$('#btn').on('click', function(event) {
  $(this).prop('disabled', true);
  console.log('Botão pressionado');
  
  // processando o evento click...
  
  $(this).prop('disabled', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="btn">Pressione-me!</button>
    
06.06.2018 / 13:44