I am new to webworker but I managed to send xmlhttprequest to my rest api and I got json back. But I want send this request again and again (in a loop), until the page is ac
You can use EventSource to get stream from server until .close()
is called at Worker
, or message is passed to Worker
signalling Worker
to call .close()
.
const es = new EventSource("/path/to/server");
es.addEventListener("open", function(event) {
console.log("event source open")
});
es.addEventListener("message", function(event) {
// do stuff with `event.data`
console.log(event.data);
});
es.addEventListener("error", function(event) {
console.log("event source error", event)
});
button.addEventListener("click", function() {
es.close();
});