diff --git a/static/js/tasks.js b/static/js/tasks.js index b910252..8150f84 100644 --- a/static/js/tasks.js +++ b/static/js/tasks.js @@ -69,16 +69,18 @@ async function loadTasks() { } } - // Directly set in-focus on the scrolled task instead of recalculating - if (focusTaskId) { - document.querySelectorAll(".task-item.in-focus").forEach(el => el.classList.remove("in-focus")); - const focusEl = document.querySelector(`[data-task-id="${focusTaskId}"]`); - if (focusEl) { - focusEl.classList.add("in-focus"); + // Wait one frame so the async scroll event from scrollToTask fires first, + // then set in-focus and bind handlers after it has been consumed. + requestAnimationFrame(() => { + if (focusTaskId) { + document.querySelectorAll(".task-item.in-focus").forEach(el => el.classList.remove("in-focus")); + const focusEl = document.querySelector(`[data-task-id="${focusTaskId}"]`); + if (focusEl) { + focusEl.classList.add("in-focus"); + } } - } - - initScrollFocus(); + initScrollFocus(); + }); } catch (error) { console.error("Failed to load tasks:", error); } @@ -170,12 +172,8 @@ function initScrollFocus() { scrollView.removeEventListener("scroll", handleScrollFocus); scrollView.removeEventListener("scroll", handleScrollSave); - - // Delay binding to skip the async scroll event queued by scrollToTask - setTimeout(() => { - scrollView.addEventListener("scroll", handleScrollFocus); - scrollView.addEventListener("scroll", handleScrollSave); - }, 0); + scrollView.addEventListener("scroll", handleScrollFocus); + scrollView.addEventListener("scroll", handleScrollSave); } function handleScrollFocus() {