]> git.ardis.lu - css-for-line-clamp-ellipsis/commitdiff
Add debounce section
authorArdis Lu <ardisglu@gmail.com>
Mon, 10 Apr 2023 03:27:42 +0000 (20:27 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 10 Apr 2023 03:27:42 +0000 (20:27 -0700)
index.html

index 50af36dd185a8e9c8cee01417c481d86735ce050..2e33ccc2b46b366ff97d0bc18ea5959de113ca95 100644 (file)
@@ -204,9 +204,29 @@ new ResizeObserver(_ => elem.style.setProperty('--lines', Math.floor(elem.client
         limitation of <code>line-clamp</code>.
       </p>
     </div>
+    <p>It's helpful to debounce <code>ResizeObserver</code>'s event listener for better performance. Using this basic debounce function:</p>
+    <pre>
+function debounce(fn, wait) {
+  let timeoutId;
+  return () => {
+    clearTimeout(timeoutId);
+    timeoutId = setTimeout(fn, wait);
+  }
+}</pre>
+    <p>You can update the event listener to cancel if another event is fired within 200 milliseconds:</p>
+    <pre>
+new ResizeObserver(debounce(() => elem.style.setProperty('--lines', Math.floor(elem.clientHeight / getLineHeightInPx(elem))), 200)).observe(elem);</pre>
   </main>
 
   <script type="module">
+    function debounce(fn, wait) {
+      let timeoutId;
+      return () => {
+        clearTimeout(timeoutId);
+        timeoutId = setTimeout(fn, wait);
+      }
+    }
+
     function getLineHeightInPx(elem) {
       const temp = document.createElement('div');
       temp.textContent = '0';
@@ -217,7 +237,7 @@ new ResizeObserver(_ => elem.style.setProperty('--lines', Math.floor(elem.client
     }
 
     const elem = document.querySelector('.dynamic-line-clamp');
-    new ResizeObserver(_ => elem.style.setProperty('--lines', Math.floor(elem.clientHeight / getLineHeightInPx(elem)))).observe(elem);
+    new ResizeObserver(debounce(() => elem.style.setProperty('--lines', Math.floor(elem.clientHeight / getLineHeightInPx(elem))), 200)).observe(elem);
   </script>
 </body>
 
This page took 0.036558 seconds and 4 git commands to generate.