From 8d69357b52f064eb0df384487bc81da4dd74ca52 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 2 Feb 2017 16:06:49 +0100 Subject: [PATCH] Debounce scrollDown --- src/converse-chatview.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/converse-chatview.js b/src/converse-chatview.js index ecc17118e..7e3ced621 100644 --- a/src/converse-chatview.js +++ b/src/converse-chatview.js @@ -805,11 +805,23 @@ return this; }, - scrollDown: function () { + _scrollDown: function () { + /* Inner method that gets debounced */ if (this.$content.is(':visible') && !this.model.get('scrolled')) { this.$content.scrollTop(this.$content[0].scrollHeight); this.$el.find('.new-msgs-indicator').addClass('hidden'); } + }, + + scrollDown: function () { + if (_.isUndefined(this.debouncedScrollDown)) { + /* We wrap the method in a debouncer and set it on the + * instance, so that we have it debounced per instance. + * Debouncing it on the class-level is too broad. + */ + this.debouncedScrollDown = _.debounce(this._scrollDown, 250, {'leading': true}); + } + this.debouncedScrollDown.apply(this, arguments); return this; } });