그것은 부진 때문에 이벤트 핸들러인지 여부를 말하기 어려운, 또는 브라우저 페이지에 DOM 노드의 깎아 지른듯한 금액, 또는 다른 이유를 처리 할 수 있기 때문에 단순히.
다음은 현재 뷰포트에없는보기의 이벤트 삭제 취소를위한 빠른 해결책입니다. 프로덕션 준비는 아니지만 이벤트 처리기가 성능 문제의 원인인지 테스트하는 데 도움이됩니다.
@fencliff
var View = Backbone.View.extend({
onScroll: function() {
var wasInView = this.isInView;
var isInView = this.checkIsInView();
if(wasInView === true) {
if(!isInView) {
this.undelegateEvents();
}
}
else if(wasInView === false) {
if(isInView) {
this.delegateEvents();
}
}
else {
//first pass
if(!isInView) {
this.undelegateEvents();
}
}
this.isInView = isInView;
},
checkIsInView: function() {
var $el = this.$el,
top = $el.offset().top,
bottom = top + $el.height(),
windowTop = $(window).scrollTop(),
windowBottom = windowTop + $(window).height();
return ((bottom <= windowBottom) && (top >= windowTop));
},
render: function() {
//rendering code here...
if(!this.lazyScroll) {
//wait for scroll events to stop before triggering scroll handler
this.lazyScroll = _.debounce(_.bind(this.onScroll, this), 50);
$(window).bind('scroll', this.lazyScroll)
.bind('resize', this.lazyScroll);
}
return this;
},
remove: function() {
if(this.lazyScroll) {
$(window).unbind('scroll', this.lazyScroll)
.unbind('resize', this.lazyScroll);
delete this.lazyScroll;
}
Backbone.View.prototype.remove.apply(this, arguments);
}
});
감사 (Working JSFiddle here은 또한 브라우저 콘솔을 확인)! 나는 그것을 밖으로 시험하고 당신에게 돌아갈 것이다. 고마워요 백만 다시 :) –
나는 테스트 후이 버전을 구현! 처음에는 성능에 도움이되지 않았지만 스크롤 할 때 과도한 메모리 문제를 막을 수있었습니다. –
감사합니다. 이것은 대단합니다.) –