Function is only called on the first click of the button

Asked

Viewed 68 times

0

I have a code in which I show the count of new messages that the user has in a chat (This count is in the database).

This chat room is like a chat room.

So I made an event that when the user clicks on the icon, then his message counter is reset. I do this through an event calling a function with AJAX.

The problem is that the counter is reset only the first time I click on the icon, if the user receives new messages (without updating the page), the counter is updated, but if I click on the icon the counter is not reset again.

Can you see what’s wrong with my code? I’m going to try to make a very succinct part of the code.

$(document).ready(function() {

  //EVENTO PARA ZERAR O CONTADOR DE MENSAGEM DO CHAT AO CLICARMOS NO BOTÃO
  $(document).on("click", ".botao-chat", function(e) {

    console.log("Clicou no botão");
    var chamar = zerarMensagem();

  });


  //FUNÇÃO É CHAMADA PARA ATUALIZAR O CONTADOR DE MENSAGENS 
  function contarMensagem() {

    $.ajax({
      url: "../banco/chat/contar-mensagens.php",
      cache: false,

    }).done(function(contador) {

      $.each($.parseJSON(contador), function(chave, valor) {
        $('span.span-contador').html(valor["contador"]);

      });


    }).fail(function() {
      alert("Falha na contagem das mensagens");

    }).always(function() {});

  }

  //Aqui repitimos a função de cima a cada 5 segundo 
  var intervalo_contagem = setInterval(contarMensagem, 5000);



  //FUNÇÃO É CHAMADA PARA ZERAR O CONTADOR DE MENSAGEM QUANDO CLICAMOS NO ÍCONE DE VISUALIZAR
  function zerarMensagem() {

    $.ajax({
      url: "../banco/chat/zerar-contador.php",
      cache: false,

    }).done(function(returno) {
      /*
      	$.each($.parseJSON(returno), function(chave, valor)
      	{	
      		$('span.span-contador').html(valor["contador"]); //Já tentei valor.contador e não consegui também		
      		
      	});		
      */
      $('span.span-contador').html("0");

    }).fail(function() {
      alert("Problemas na função zerarMensagem");
    }).always(function() {});

  }

});

System image.

inserir a descrição da imagem aqui

  • The console.log("Clicou no botão"); appears on the console after the first click?

  • No, it only appears on the first click.

  • Ih, then you have no way to respond. See the "Element Inspecter" for changes to the button’s HTML after the first click. This is the typical problem that you can’t answer because there’s no way to replicate the problem. Also because it is not allowed to debug code here in the comments. Maybe another code is also conflicting... Anyway, there are many possibilities.

  • Visibly all right with the code ?

  • 1

    Apparently you’re right. For the part that matters $(document).on("click", ".botao-chat", function(e) { you’re right. If you are no longer entering this function after the first click, the part of Ajax does not need to analyze, but seems normal, since you say it works at least in the first click.

  • But thanks a lot, Sam.

Show 1 more comment

1 answer

0


Solved.

I traded:

$(document).on("click", ".botao-chat", function(e) {

  console.log("Clicou no botão");
  var chamar = zerarMensagem();

});

For:

$(".botao-chat").click(function(e) {

  console.log("Clicou no botão");
  var chamar = zerarMensagem();

});

  • According to a friend who taught me, in this case the event ceased to be propagated on the document and started to be propagated on the object.

Browser other questions tagged

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