Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

iamAyushChamoli
Copy link
Contributor

Fixes #147

Type of change:

Code Update, calling the update() method in doxygen-awesome-interactive-toc.js by throttling every 100ms instead of calling it every time the screen is scrolled.

…adings

Fixes #147
Type of change:
Code Update, calling the `update()` method in `doxygen-awesome-interactive-toc.js` by throttling every 100ms instead of calling it every time the screen is scrolled.
document.getElementById("doc-content")?.addEventListener("scroll", () => {
DoxygenAwesomeInteractiveToc.update()
})
document.getElementById("doc-content")?.addEventListener("scroll",throttle(DoxygenAwesomeInteractiveToc.update, 100))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "this" keyword is missing before the throttle method. Without this, the fix does not work and causes errors in the console.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code, please review and let me know if it works now.

Used "this" keyword before throttle.
Copy link

@atlz253 atlz253 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is working now :)

@iamAyushChamoli
Copy link
Contributor Author

Everything is working now :)

Glad to know! Thanks!

@iamAyushChamoli
Copy link
Contributor Author

@jothepro ready for review

@jothepro
Copy link
Owner

jothepro commented Jul 7, 2024

Thank you for your contribution, @iamAyushChamoli! I can confirm that this greatly reduces the amount of calls to the update function!

I am hesitant to merge this as is though, because it comes with the risk of "swallowing" events at the end of a scroll. For example, if you try scrolling really fast, for example by dragging the scrollbar all the way to the bottom of the page fast enough, the status of the TOC might not be updated correctly:

missing_scroll_events.mov

I don't think this is a major dealbreaker, because you need to scroll really fast to trigger the behavior, but I'd like to figure out if this can be avoided nevertheless.

@iamAyushChamoli
Copy link
Contributor Author

iamAyushChamoli commented Jul 7, 2024

@jothepro if I may suggest some fixes,
How about we try modifying the time period after which the 'update()' function is throttled? Currently it is 100ms, some minor modifications like upscaling to 200ms or downscaling to 50ms might help.

Alternatively, how about batching the changes that occur during the scroll event and making the classname changes in the end?

@jothepro
Copy link
Owner

jothepro commented Jul 8, 2024

Sure, I am open for suggestions, I am unsure on how this could best be fixed atm!

My hot take would be to delay the execution of the updates:

    static throttle(func, delay) {
        let lastCall = 0;
        return function (...args) {
            const now = new Date().getTime();
            if (now - lastCall < delay) {
                return;
            }
            lastCall = now;
            return setTimeout(() => {func(...args)}, delay);
        };
    }

It makes the TOC updates lag by 100ms, but I don't think anybody would notice. What you think about that solution?

@iamAyushChamoli
Copy link
Contributor Author

iamAyushChamoli commented Jul 8, 2024

@jothepro Sure, I think this solution might work since we are already talking about edge case scenarios like scrolling really fast so it is very less likely that this problem will be reproduced frequently. However, if it does, the proposed solution might be a good fix at a negligible cost of TOC update by 100ms. Have you tried testing it out? If you have, can you attach a screen recording so that I can see it for myself if it is noticeable or not? (not sure, but I think you have tried coupling throttling with debouncing in this version? Anyways, if it works, it can be a good addition)

@atlz253
Copy link

atlz253 commented Jul 8, 2024

@jothepro @iamAyushChamoli Yes, fix work. I think this behavior is enough for normal work.

output.compress-video-online.com.mp4

@iamAyushChamoli
Copy link
Contributor Author

Thanks @atlz253 for testing it out 🥳, @jothepro should I commit these changes?

Updated the throttle function with jothepro's solution.
Copy link
Owner

@jothepro jothepro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your contribution! I will merge and release this ASAP!

Comment on lines 81 to 90
let lastCall = 0;
return function (...args) {
const now = new Date().getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return func(...args);
};
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation seems a little bit off.

Suggested change
let lastCall = 0;
return function (...args) {
const now = new Date().getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return func(...args);
};
}
let lastCall = 0;
return function (...args) {
const now = new Date().getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return func(...args);
};
}

@jothepro jothepro merged commit 28ed396 into jothepro:main Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lags when scrolling on a page with TOC and a large number of headings
3 participants