Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions simplq/src/utils/scrollingOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ export const smoothScrollToHomePageTop = (history) => {

/**
* Execute a callback as soon as an element is available on the DOM.
* The function will stop checking after 5 seconds to prevent memory leaks.
*
* id - element id to wait on
* callback - callback to execute as soon as the element becomes available. The
* element is passed to the callback and it is triggered.
* @param {string} id - element id to wait on
* @param {Function} callback - callback to execute as soon as the element becomes available.
* The element is passed to the callback when it is triggered.
* */
export const onLoadById = (id, callback) => {
const maxAttempts = 50; // 50 attempts * 100ms = 5 seconds
let attempts = 0;

const checkAndExecute = setInterval(() => {
const element = document.getElementById(id);
attempts += 1;

if (element) {
callback(element);
clearInterval(checkAndExecute);
} else if (attempts >= maxAttempts) {
// Element not found after timeout, clear interval to prevent memory leak
clearInterval(checkAndExecute);
}
}, 100);
};