我需要视图来重新绘制集合并每30秒重新渲染一次。问题是,一旦我改变页面(没有完整页面刷新),setInterval保留在内存上,并在后台继续重新绘制。然而这种观点早已被摧毁。骨干:使用setInterval
代码:
define(
[
"underscore",
"collection/system/feed",
"view/list",
"text!template/index/system-feed.html"
],
function (_, Collection, ListView, Template) {
return ListView.extend({
el: '<div id="main-inner">',
collection: new Collection(),
loading: true,
client: false,
initialize: function (options) {
this.collection.fetch();
this.collection.on('reset', this.onFetchCollection, this);
var self = this;
setInterval(function() {
self.collection.fetch();
}, 30000);
},
/*collection is returned, so reset the loading symbol and set the posts to the render*/
onFetchCollection: function() {
this.list = this.collection.toJSON();
this.loading = false;
this.render();
},
render: function() {
var html = _.template(Template, {loading: this.loading, client: this.client, list: this.list});
this.$el.html(html);
return this;
}
});
}
);
你知道*何时该视图被“销毁?”例如,如果在视图上调用remove(),则可以覆盖它并使用'clearInterval'来停止您的循环获取。 – Divey
大约有55页,所以使用clearInterval会很困难,除非我使用间隔的标准名称。我猜。 – azz0r
但是,每次关闭视图时(特别是当您有55页时),您确实应该调用'view.remove()'。除非泄漏是您的应用程序的功能之一。而'collection:new Collection()'不会做你可能认为它做的事。 –