Javascript 当路由更改时,从主干视图中清除间隔

Javascript 当路由更改时,从主干视图中清除间隔,javascript,backbone.js,Javascript,Backbone.js,在这个视图中我有一个间隔 var QuestionsView = Backbone.View.extend({ render: function(){ var view = this; this.updateQuestionsIntervalId = setInterval(function() {view.refreshQuestionLists()}, 3000); ), refreshQuestionLists: function(

在这个视图中我有一个间隔

var QuestionsView = Backbone.View.extend({
    render: function(){
        var view = this;
        this.updateQuestionsIntervalId = setInterval(function() {view.refreshQuestionLists()}, 3000);
    ),

    refreshQuestionLists: function() {
        this.questions.fetch({ 
            ...
        });
    },

    navigateAway: function() {
        clearInterval(this.updateQuestionsIntervalId);
    }

}); 
理想情况下,我希望在路线更改时运行
QuestionView.navigateAway()
。还有什么我可以做的吗


感谢作为一种省力的解决方案,您可以将view方法直接绑定到
路由器:路由
事件,每当路由器将URL更改匹配到任何定义的路由时,该事件都会触发:

var QuestionsView = Backbone.View.extend({
  initialize: function(){
    this.listenTo(yourRouter, 'route', this.navigateAway); 
  )
}); 
这应该行得通,但对我来说就像意大利面条

我通常在视图上实现了一个
onBeforeClose
方法,在离开当前视图之前调用该方法。有点像:

var Router = Backbone.Router.extend({
  navigateToView: function(view) {
    //I've actually abstracted this logic to a separate
    //view manager class, but you get the idea...
    var current = this.currentView; 
    if(current) {
      if(current.onBeforeClose)
        current.onBeforeClose();
      current.remove();
    }

    this.currentView = view;
    //render your current view here, however you like
    $(body).html(this.currentView.render().el);
  },

  someRoute: function() {
    var view = new SomeView();
    this.navigateToView(view);
  }
});
这更像是一个惯例。如果视图没有onBeforeClose方法,则不会调用它,也不会造成任何伤害


请注意,这要求您使用一种集中的方法呈现视图(在本例中为
navigateToView
),但这是一件好事,因为您应该使用
remove
清除旧视图。作为一种省力的解决方案,您可以将view方法直接绑定到
router:route
事件,每次路由器将URL更改与任何已定义的路由匹配时激发:

var QuestionsView = Backbone.View.extend({
  initialize: function(){
    this.listenTo(yourRouter, 'route', this.navigateAway); 
  )
}); 
这应该行得通,但对我来说就像意大利面条

我通常在视图上实现了一个
onBeforeClose
方法,在离开当前视图之前调用该方法。有点像:

var Router = Backbone.Router.extend({
  navigateToView: function(view) {
    //I've actually abstracted this logic to a separate
    //view manager class, but you get the idea...
    var current = this.currentView; 
    if(current) {
      if(current.onBeforeClose)
        current.onBeforeClose();
      current.remove();
    }

    this.currentView = view;
    //render your current view here, however you like
    $(body).html(this.currentView.render().el);
  },

  someRoute: function() {
    var view = new SomeView();
    this.navigateToView(view);
  }
});
这更像是一个惯例。如果视图没有onBeforeClose方法,则不会调用它,也不会造成任何伤害


请注意,这要求您使用一种集中式方法呈现视图(在本例中为
navigateView
),但这是一件好事,因为您应该使用
remove
来清理旧视图,所以不管怎样。

我需要这样做的原因是,通过停止上一个间隔来停止视图渲染时开始的多个间隔

但我至少需要一次跑步

所以我走错了路,解决方案是:

var QuestionsView = Backbone.View.extend({

    updateQuestionsIntervalId: false,

    render: function(){
        var view = this;

        if(this.updateQuestionsIntervalId == false)
            this.updateQuestionsIntervalId = setInterval(function() {view.refreshQuestionLists()}, 3000);
    ),

    refreshQuestionLists: function() {
        this.questions.fetch({ 
            ...
        });
    }

}); 

我需要这样做的原因是,通过停止上一个间隔来停止渲染视图时开始的多个间隔

但我至少需要一次跑步

所以我走错了路,解决方案是:

var QuestionsView = Backbone.View.extend({

    updateQuestionsIntervalId: false,

    render: function(){
        var view = this;

        if(this.updateQuestionsIntervalId == false)
            this.updateQuestionsIntervalId = setInterval(function() {view.refreshQuestionLists()}, 3000);
    ),

    refreshQuestionLists: function() {
        this.questions.fetch({ 
            ...
        });
    }

});