eco2d/flecs-dash/etc/js/periodic_request.js

37 lines
788 B
JavaScript
Raw Normal View History

2021-01-18 16:40:47 +00:00
class PeriodicRequest {
constructor(interval, callback, err_callback) {
this.error = false;
this.timer = undefined;
this.result = undefined;
this.interval = interval;
this.callback = callback;
this.err_callback = err_callback;
}
request(url) {
app.get(url, (msg) => {
this.result = msg;
this.error = false;
this.callback(msg);
}, (Http) => {
if (!Http.status || Http.status >= 400) {
this.err_callback(Http.status);
this.error = true;
}
});
}
stopRequesting() {
this.result = undefined;
clearInterval(this.timer);
}
startRequesting(url) {
this.stopRequesting();
this.timer = window.setInterval(function() {
this.request(url);
}.bind(this), this.interval);
}
}