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

134
Vistas
Javascript method to alert the user when a method is taking longer than expected?

I have some socket.io emits that I'd like to provide handling for when they are taking too long to get back from the server with their promise. But I also don't want to outright time them out. I have seen this on some websites where a loading spinner will add 'This seems to be taking longer than usual' text after 10 or so seconds.

How can I add a timeout-style method to a Javascript function that will do something after a certain amount of time, but will NOT cancel the original method?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Unless you have a global scope, you'll need to pass in two handlers

Something like this (I've generalised the flow because you've not provided enough information about your existing set up, so I hope it makes sense)

function doSocketRequest(normalHandler, tooLongHandler) {
    let timer = null;
    
    // set timeout to 5 seconds, if we hit it then trigger "tooLongHandler"
    timer = setTimeout(tooLongHandler, 5000);
    
    // When we receive our socket response for the event, clear the time and trigger "normalHandler"
    socket.on('some_event_response', (response) => {
        clearTimeout(timer);
        normalHandler(response)
    });
    
    // Emit to server
    socket.emit('some_event', ...etcetc);   
}
about 4 years ago · Juan Pablo Isaza 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