在我的javascript中我定义了一个轮询器,以便每秒刷新一些内容。定义如下:
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();
});
问题是加载页面时它不会立即启动。有谁知道原因?非常感谢提前!