Skip to content

Commit 8dc320e

Browse files
committed
Optmize scroll event handler performance through debounce #49
1 parent 9bf3607 commit 8dc320e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/components/InfiniteLoading.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
isLoading: false,
7373
isComplete: false,
7474
isFirstLoad: true, // save the current loading whether it is the first loading
75+
debounceTimer: null,
76+
debounceDuration: 100,
7577
};
7678
},
7779
computed: {
@@ -112,9 +114,15 @@
112114
mounted() {
113115
this.scrollParent = getScrollParent(this.$el);
114116
115-
this.scrollHandler = function scrollHandlerOriginal() {
117+
this.scrollHandler = function scrollHandlerOriginal(ev) {
116118
if (!this.isLoading) {
117-
this.attemptLoad();
119+
clearTimeout(this.debounceTimer);
120+
121+
if (typeof ev === 'object' && ev.constructor === Event) {
122+
this.debounceTimer = setTimeout(this.attemptLoad, this.debounceDuration);
123+
} else {
124+
this.attemptLoad();
125+
}
118126
}
119127
}.bind(this);
120128

0 commit comments

Comments
 (0)