FIX: prevents multiple jumps to happen with LockOn (#8460)

FIX: prevents multiple jumps to happen with LockOn (#8460)

diff --git a/app/assets/javascripts/discourse/lib/lock-on.js.es6 b/app/assets/javascripts/discourse/lib/lock-on.js.es6
index a2ea547ea3..13720bfeec 100644
--- a/app/assets/javascripts/discourse/lib/lock-on.js.es6
+++ b/app/assets/javascripts/discourse/lib/lock-on.js.es6
@@ -51,23 +51,25 @@ export default class LockOn {
   lock() {
     const startedAt = new Date().getTime();
     let previousTop = this.elementTop();
+    previousTop && $(window).scrollTop(previousTop);
 
     const interval = setInterval(() => {
-      if (!previousTop) {
-        previousTop = this.elementTop();
-        previousTop && $(window).scrollTop(previousTop);
-      } else {
-        const top = Math.max(0, this.elementTop());
-        const scrollTop = $(window).scrollTop();
+      const elementTop = this.elementTop();
+      if (!previousTop && !elementTop) {
+        // we can't find the element yet, wait a little bit more
+        return;
+      }
 
-        if (typeof top === "undefined" || isNaN(top)) {
-          return this.clearLock(interval);
-        }
+      const top = Math.max(0, elementTop);
+      const scrollTop = $(window).scrollTop();
 
-        if (!within(4, top, previousTop) || !within(4, scrollTop, top)) {
-          $(window).scrollTop(top);
-          previousTop = top;
-        }
+      if (typeof top === "undefined" || isNaN(top)) {
+        return this.clearLock(interval);
+      }
+
+      if (!within(4, top, previousTop) || !within(4, scrollTop, top)) {
+        $(window).scrollTop(top);
+        previousTop = top;
       }
 
       // Commit suicide after a little while

GitHub sha: c5140ef3

1 Like