diff --git a/simplq/src/utils/scrollingOperations.js b/simplq/src/utils/scrollingOperations.js index 00fe8921..3f68a2bf 100644 --- a/simplq/src/utils/scrollingOperations.js +++ b/simplq/src/utils/scrollingOperations.js @@ -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); };