From d7f6d6a7734b658b28c543b403810d397bbb6070 Mon Sep 17 00:00:00 2001 From: Reorx Date: Sun, 10 Apr 2022 17:47:41 +0800 Subject: [PATCH] use setTimeout to reduce scroll callback frequency --- layouts/partials/footer.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index 9415aaa..a9d9d7b 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -146,7 +146,7 @@ window.addEventListener('DOMContentLoaded', function (event) { let activeHeading = headings[0]; getLinkByHeading(activeHeading).classList.add(activeClass); - window.addEventListener('scroll', () => { + const onScroll = () => { const passedHeadings = []; for (const h of headings) { // 5 px as a buffer @@ -166,6 +166,14 @@ window.addEventListener('DOMContentLoaded', function (event) { activeHeading = newActiveHeading; getLinkByHeading(activeHeading).classList.add(activeClass); } + } + + let timer = null; + window.addEventListener('scroll', () => { + if (timer !== null) { + clearTimeout(timer) + } + timer = setTimeout(onScroll, 50) }, false); function getLinkByHeading(heading) {