Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!