Detect change of readyState with SSE
-
21-12-2019 - |
Question
I am using HTML5 Server-Sent events:
SSEUpdate = new EventSource("update.php");
I want to detect when SSEUpdate.readyState
changes. I've tried .onerror
, but when I use console.log(SSEUpdate.readyState)
, I always get 0
. I want to run my function when readyState
changes to 2
. Thank you!
Solution
I ended up using a polling solution:
var SSECheck = setInterval(function() {
if (SSEUpdate.readyState == 2) {
clearInterval(SSECheck);
// call to fn to restart sse
}
}, 500);
Takes up to 0.5s to capture, but it does the job.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow