Skip to content

Commit 299999d

Browse files
authored
Merge pull request #163 from brich-dev-member/dist_rollback
Use throttle instead of debounce
2 parents 24aeeab + eace9cc commit 299999d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/components/InfiniteLoading.vue

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767
isLoading: false,
6868
isComplete: false,
6969
isFirstLoad: true, // save the current loading whether it is the first loading
70-
debounceTimer: null,
71-
debounceDuration: 50,
70+
inThrottle: false,
7271
infiniteLoopChecked: false, // save the status of infinite loop check
7372
infiniteLoopTimer: null,
7473
continuousCallTimes: 0,
@@ -109,16 +108,22 @@
109108
default: 'bottom',
110109
},
111110
forceUseInfiniteWrapper: null,
111+
throttleLimit: {
112+
type: Number,
113+
default: 50,
114+
},
112115
},
113116
mounted() {
114117
this.scrollParent = this.getScrollParent();
115118
116119
this.scrollHandler = function scrollHandlerOriginal(ev) {
117120
if (!this.isLoading) {
118-
clearTimeout(this.debounceTimer);
119-
120121
if (ev && ev.constructor === Event) {
121-
this.debounceTimer = setTimeout(this.attemptLoad, this.debounceDuration);
122+
if (!this.inThrottle) {
123+
this.inThrottle = true;
124+
this.attemptLoad();
125+
setTimeout(() => { this.inThrottle = false; }, this.throttleLimit);
126+
}
122127
} else {
123128
this.attemptLoad();
124129
}
@@ -160,6 +165,7 @@
160165
this.isLoading = false;
161166
this.isComplete = false;
162167
this.isFirstLoad = true;
168+
this.inThrottle = false;
163169
this.scrollParent.addEventListener('scroll', this.scrollHandler);
164170
setTimeout(this.scrollHandler, 1);
165171
});

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ describe('vue-infinite-loading', () => {
404404
vm.$mount('#app');
405405
});
406406

407-
it('should debounce properly for the scroll event handler\n (use div as the container and wave dots spinner)', (done) => {
407+
it('should throttle properly for the scroll event handler\n (use div as the container and wave dots spinner)', (done) => {
408408
vm = new Vue(Object.assign({}, basicConfig, {
409409
data: {
410410
list: [...new Array(20).join('1').split('')],
@@ -420,11 +420,11 @@ describe('vue-infinite-loading', () => {
420420
continuesCall(() => {
421421
scrollParent.scrollTop += 10;
422422
}, 10, () => {
423-
expect(spyFn).to.have.been.callCount(0 + alreadyCalledTimes);
423+
expect(spyFn).to.have.been.callCount(1 + alreadyCalledTimes);
424424
setTimeout(() => {
425425
expect(spyFn).to.have.been.callCount(1 + alreadyCalledTimes);
426426
done();
427-
}, this.$refs.infiniteLoading.debounceDuration + 10);
427+
}, this.$refs.infiniteLoading.throttleLimit + 10);
428428
});
429429
},
430430
}));

0 commit comments

Comments
 (0)