Error executing function passed via parameter in javascript

Asked

Viewed 40 times

1

I have this modal functionConfirmation that I’m trying to use for various functions. In it I pass the title and the modal message and the function to be executed if the user click OK.

function modalConfirmacao(titulo, mensagem,funcao) {
    var htmlModal = '<div class="modal fade" id="modalDefault" tabindex="-1" role="dialog"  aria-hidden="true">' +
                        '<div class="modal-dialog">'+
                            '<div class="modal-content">'+
                                '<!-- Modal Header -->'+
                                '<div class="modal-header">'+
                                    '<h4 class="modal-title" id="myModalLabel">'+
                                        titulo
                                    +'</h4>'+
                                    '<button type="button" class="close" data-dismiss="modal">'+
                                        '&times;'+
                                    '</button>'+
                                '</div>'+
                                '<!-- Modal Body -->'+
                                '<div class="modal-body">'+
                                    mensagem
                                +'</div>'+
                                '<!-- Modal Footer -->'+
                                '<div class="modal-footer">'+
                                    '<button type="button" id="ok" class="btn btn-primary" data-dismiss="modal">'+
                                        'OK'+
                                    '</button>'+
                                    '<button type="button" id="cancelar" class="btn btn-default" data-dismiss="modal">'+
                                        'Cancelar'+
                                    '</button>'+
                                '</div>'+
                            '</div>'+
                        '</div>' +
                    '</div>';
            $('body').append(htmlModal);
            $("#modalDefault").modal();
            $("#ok").on("click",funcao);
}

To call the function I do so on the onclick of the button:

modalConfirmacao("Confirmar","Tem certeza que deseja excluir o produto "+codigo+"?",excluirProduto);

The excluirProduct function has an Ajax call in php to delete the product.

function excluirProduto(codigo){
            $.post('excluirproduto.php',{codigo:codigo},function(e){
                e = $.parseJSON(e);
                if(e){
                    alert("Produto excluido");

                }else{
                    alert("Erro ao excluir produto");
                }

            });
        }

My problem is that when I click the button that calls the function excluirProduct for the first time it runs the function 1 time, the second time 2 time, the third time 3 times and so on. What could be going on? I am passing or calling the function erroneously?

2 answers

0

You could also use some Javascript functions to run a function just once, then read about the Throttle and Debounceat that link: Throttling and Debouncing

0


The on() jQuery accumulates event handlers, that is, each time it is called to modalConfirmacao(), it will turn on the event handler again on click of the OK button.

You would have to change the code to perform the event call click with the OK button only once, or use off(), to "disconnect" a manipulator:

$("#ok").off("click").on("click", funcao);
  • Thanks for your attention. The suggestion of off worked perfectly!!! You also suggested to change the code to link to the event click only once. How would you do this?

  • On second thought, maybe there’s no way, really.

  • Then within the function prepare Dal I would have : $("#ok"). on("click",function?

  • From what I understood then, every time I call the modal functionConfirmation he was putting a new event, there was accumulating making the function perform the number of times the button was clicked, more or less that? I think I understand but I can’t explain...

  • Within modalConfirmacao() can have a function, for example, function estadoSetado() { return modalConf.estado; } and can have two variables, var estado; and var modalConf = this;. Hence, on the call from prepararModal(func), you do if (!estadoSetado()) { preparaModal(excluirProduto); estado = true; }... Got complicated :(

  • No; like the $("#ok").on("click", funcao); was being called several times, every round of this line, he added again the excluirProduto. Got it?

  • Got it, perfect. Thank you!!!!

Show 2 more comments

Browser other questions tagged

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