From: Ardis Lu Date: Mon, 10 Apr 2023 03:27:42 +0000 (-0700) Subject: Add debounce section X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=ccb14e472f0b40cffd3ee6bda87a38ab42ae8554;p=css-for-line-clamp-ellipsis Add debounce section --- diff --git a/index.html b/index.html index 50af36d..2e33ccc 100644 --- a/index.html +++ b/index.html @@ -204,9 +204,29 @@ new ResizeObserver(_ => elem.style.setProperty('--lines', Math.floor(elem.client limitation of 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);