Skip to content

Instantly share code, notes, and snippets.

@32teeth
Created May 8, 2023 15:45
Show Gist options
  • Save 32teeth/fdcddc16a16cd7c6536880b6c54f2f48 to your computer and use it in GitHub Desktop.
Save 32teeth/fdcddc16a16cd7c6536880b6c54f2f48 to your computer and use it in GitHub Desktop.
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