Double event on jQuery click

Asked

Viewed 198 times

2

I am on time having problems with jQuery events. I have used all the best practices to stop the double event of click. Following are examples:

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

Always when there is a click I perform 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?

  • It seems that your event is being called twice, the .off you said you used, was it time to create the event? For example, try to do $("#button"). off("click"). on("click",Function(){ /your stuff here/ }); to prevent it from being created twice or more. If this does not work you can try something more complex, something like saving your button when it is clicked and only allow clicks again after, say, 1 second. If I need I can set you an example.

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

1 answer

0

You can try with the function one jQuery. It is similar to the function on, except that the event is removed from the element only its first execution, i.e., for the example below the function in the event click 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>
<button id="btn">Pressione-me!</button>

If it is possible that the event is triggered multiple times on the same page and the intention is only to protect from multiple clicks simultaneous, 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>
<button id="btn">Pressione-me!</button>

  • However this button will run again on the same screen, because the application is based on ajax, you can renew this btn without updating the page ?

  • I used this control to disable the button more often in the second click my Function that this being called twice, may have been some trace of the event on the page for it to be fired twice ?

  • @Guilhermezio manages to make a [mcve]?

  • I always use when I click this function function LoadingTela(botao,tipo) {&#xA; if (tipo == true) {&#xA; $(botao).prop("disabled", true);&#xA; $('.mask-loading').show();&#xA; } else {&#xA; $(botao).prop("disabled", false);&#xA; $('.mask-loading').hide();&#xA; } &#xA;}

  • edited the question with the example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.