From ccb14e472f0b40cffd3ee6bda87a38ab42ae8554 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sun, 9 Apr 2023 20:27:42 -0700 Subject: [PATCH] Add debounce section --- index.html | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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);
-- 2.47.3