Created
May 8, 2023 15:45
-
-
Save 32teeth/fdcddc16a16cd7c6536880b6c54f2f48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MAX_LOOP_DURATION = 100; // Maximum duration of the ResizeObserver loop in ms | |
let resizeTimeout; | |
let resizeStartTime; | |
const observer = new ResizeObserver(entries => { | |
if (resizeTimeout) { | |
clearTimeout(resizeTimeout); | |
} | |
// If the ResizeObserver loop has been running for more than the maximum duration, bail out | |
if (resizeStartTime && Date.now() - resizeStartTime > MAX_LOOP_DURATION) { | |
console.warn('ResizeObserver loop limit exceeded'); | |
return; | |
} | |
resizeStartTime = Date.now(); | |
resizeTimeout = setTimeout(() => { | |
// handle the ResizeObserver event | |
resizeStartTime = null; | |
}, 0); | |
}); | |
// When the observer is disconnected, clear any remaining timeouts | |
observer.disconnect = function() { | |
clearTimeout(resizeTimeout); | |
ResizeObserver.prototype.disconnect.apply(this, arguments); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment