From: Ardis Lu line-clamp.
It's helpful to debounce ResizeObserver's event listener for better performance. Using this basic debounce function:
+function debounce(fn, wait) {
+ let timeoutId;
+ return () => {
+ clearTimeout(timeoutId);
+ timeoutId = setTimeout(fn, wait);
+ }
+}
+ You can update the event listener to cancel if another event is fired within 200 milliseconds:
+
+new ResizeObserver(debounce(() => elem.style.setProperty('--lines', Math.floor(elem.clientHeight / getLineHeightInPx(elem))), 200)).observe(elem);