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

128
Visualizações
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 Respostas
Responde à pergunta

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 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