in my javascript I defined a poller in order to refresh some contents every second. The definition is the following:
var poller = {
// number of failed requests
failed: 0,
// starting interval - 1 seconds
interval: 1000,
// kicks off the setTimeout
init: function() {
setTimeout(
$.proxy(this.getData, this), // ensures 'this' is the poller obj inside getData, not the window object
this.interval
);
},
// get AJAX data + respond to it
getData: function() {
var self = this;
$.ajax({
url: "api/view",
success: function(response) {
// ....do something....
// recurse on success
self.init();
}
},
error: $.proxy(self.errorHandler, self)
});
},
// handle errors
errorHandler: function() {
if (++this.failed < 10) {
this.interval += 1000;
// recurse
this.init();
}
}
};
poller.init();
});
The problem is that it does not start immediately when the page is loaded. Does anyone know the reason why? Many thanks in advance!