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

129
Vistas
EventSource for fixed seconds in Javascript

I'm newbie to the feature of EventSource in javascript. As per my understanding the eventsource calls the api with stream content and gets the updates of data automatically without making any further calls. But I'm planning to make if possible to stop listening the response after 10 seconds from the EventSource. For Example in the below URL we can find the EventSource example, I want it to get stopped listening/fetching after 10 seconds.

https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_sse

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

0

You could use EventSource.close() method and call it in a setTimeout() such as follows (adapted from the linked W3Schools code):

if(typeof(EventSource) !== "undefined") {
  const source = new EventSource("demo_sse.php");
  source.onmessage = function(event) {
    console.log(event.data);
  };
  
  setTimeout(() => {
    source.close();
    console.log("connection closed");
  }, 10000)
  
} else {
  console.log("Sorry, your browser does not support server-sent events...");
}
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