Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
How to check if someone is currently on the website (online) using JS?

I am building a website with chat etc. The problem is I cannot correctly define if someone is currently on the website which cause I cannot set their activity.

I wrote a function which is invoked by the body tag in HTML file <body onload="changeStatus(true)" onunload="changeStatus(false)"> but as I said it seems not to work.

When the status of the first users sets to the online value, the second one immediately changes to offline. When I try to type something in the input on the website it also changes the status to offline, it doesn't matter who is typing.

I am currently thinking of writing this part in PHP just using the session, but maybe someone knows how to do this in JS?

function changeStatus(event) {
  if (event) {
    db.ref("status/" + username + "/" + receiver).set({
      act: "online"
    });
  } else {
    db.ref("status/" + username + "/" + receiver).set({
      act: "offline"
    });
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Are you trying to determine if the page has loaded? If so then a simple jquery document load on will do that

https://learn.jquery.com/using-jquery-core/document-ready/

  <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $( window ).on( "load", function() {
        console.log( "window loaded" );
    });
//or
$(function() {
    console.log( "ready!" );
});
    </script>

if your trying to capture if the user is on the page then mouse over or mouse click might be better

https://api.jquery.com/mouseover/

https://api.jquery.com/click/

I hope this helps

about 4 years ago · Juan Pablo Isaza Relatório

0

It sounds like you're trying to build a presence system, in which case I recommend checking out the Firebase documentation on managing presence, how onDisconnect works, detecting connection state and the sample presence app.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda