Selaa lähdekoodia

Debounce scrollDown

JC Brand 8 vuotta sitten
vanhempi
commit
8d69357b52
1 muutettua tiedostoa jossa 13 lisäystä ja 1 poistoa
  1. 13 1
      src/converse-chatview.js

+ 13 - 1
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;
                 }
             });