Skip to content

Use throttle instead of debounce (with rollback changes of dist file) #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/components/InfiniteLoading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
isLoading: false,
isComplete: false,
isFirstLoad: true, // save the current loading whether it is the first loading
debounceTimer: null,
debounceDuration: 50,
inThrottle: false,
infiniteLoopChecked: false, // save the status of infinite loop check
infiniteLoopTimer: null,
continuousCallTimes: 0,
Expand Down Expand Up @@ -109,16 +108,22 @@
default: 'bottom',
},
forceUseInfiniteWrapper: null,
throttleLimit: {
type: Number,
default: 50,
},
},
mounted() {
this.scrollParent = this.getScrollParent();

this.scrollHandler = function scrollHandlerOriginal(ev) {
if (!this.isLoading) {
clearTimeout(this.debounceTimer);

if (ev && ev.constructor === Event) {
this.debounceTimer = setTimeout(this.attemptLoad, this.debounceDuration);
if (!this.inThrottle) {
this.inThrottle = true;
this.attemptLoad();
setTimeout(() => { this.inThrottle = false; }, this.throttleLimit);
}
} else {
this.attemptLoad();
}
Expand Down Expand Up @@ -160,6 +165,7 @@
this.isLoading = false;
this.isComplete = false;
this.isFirstLoad = true;
this.inThrottle = false;
this.scrollParent.addEventListener('scroll', this.scrollHandler);
setTimeout(this.scrollHandler, 1);
});
Expand Down
6 changes: 3 additions & 3 deletions test/unit/specs/InfiniteLoading.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('vue-infinite-loading', () => {
vm.$mount('#app');
});

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