Prevent the event from being triggered 2 times

0

When I ask to open a modal for the first time it does the actions correctly, but when I close and open the modal again it performs the function twice, how do I avoid this?

function acoesModal(){
    $('div#fixa').css('z-index', '0');
    $('.back-captcha').slideDown(500);
    $('.captcha').fadeIn(500);

    $('body').on('click','.captcha .fechar,.captcha button#retorno',function(event) {
            event.preventDefault();             
            $('div#fixa').removeAttr('style');
            $('.back-captcha').slideUp(500);
            $('.captcha').fadeOut(300);
            $('.captcha form textarea').val('');
        });
    
asked by anonymous 22.01.2015 / 13:45

1 answer

1

There are two ways.

Using the one function.

$('body').one('click', function() {
    console.log('Esta função será executada apenas uma vez');
});

Using off .

// removo todos os eventos do body com o namespace
// .fecharModal antes de adicioná-los novamente
$('body').off('.fecharModal').on('click.fecharModal', function() {
    console.log('Esta função será executada apenas uma vez');
});

The first form works well in cases where the event will happen only once. The second one can be used in cases where the event can occur several times.

    
22.01.2015 / 13:53