From 1a63f1538212307b19fb42e739872c754d07feb9 Mon Sep 17 00:00:00 2001 From: Brock Whittaker Date: Thu, 26 Jan 2017 10:57:27 -0800 Subject: [PATCH] Only run popovers.hide_all() once on scroll start. This function throttles the function and only allows the on scroll event to fire the popovers.hide_all() function once on scroll start (determined as > 250ms after the last scroll event fire on .app. This should resolve some performance issues surrounding constantly firing queries and potentially changing the document tree. --- static/js/popovers.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/static/js/popovers.js b/static/js/popovers.js index c8600b05c2..08e791fc24 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -848,9 +848,23 @@ exports.register_click_handlers = function () { }, true); }); - $('.app').on('scroll', function () { - popovers.hide_all(); - }); + (function () { + var last_scroll = 0; + + $('.app').on('scroll', function () { + var date = new Date().getTime(); + + // only run `popovers.hide_all()` if the last scroll was more + // than 250ms ago. + if (date - last_scroll > 250) { + popovers.hide_all(); + } + + // update the scroll time on every event to make sure it doesn't + // retrigger `hide_all` while still scrolling. + last_scroll = date; + }); + }()); };