Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Javascript/jQuery beforeunload and unload not firing

I have a page where I want my script to let the server know that the user went offline when the browser/tab/page is closed.

$(document).ready(function() {
    // Inform the server that the user went online
    setStatus('online');

    // Inform the server that the user went offline
    $(window).on('beforeunload', function() {
        setStatus('offline');
    });

    // Inform the server that the user went offline
    $(window).on('unload', function() {
        setStatus('offline');
    });
});

async function setStatus(status) {
    let { data } = await axios.post('/status', { status: status });
}

The part where I have setStatus('online'); works but I can never seem to set the status to offline when I close the page/tab/browser.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Looking around, I found this really nice answer - I just had to put this on my server.js file:

peer.oniceconnectionstatechange = function() {
    if(peer.iceConnectionState == 'disconnected') {
        console.log('Disconnected');
    }
}

It does take a few seconds before it registers that the peer has dropped but at least it's always firing and that's reliable enough for me.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda