]> git.ardis.lu - event-hashes/commitdiff
Actually debounce button icon changes
authorArdis Lu <ardisglu@gmail.com>
Fri, 18 Apr 2025 06:30:59 +0000 (23:30 -0700)
committerArdis Lu <ardisglu@gmail.com>
Fri, 18 Apr 2025 06:30:59 +0000 (23:30 -0700)
Previously, clicking a button multiple times within 2 seconds resulted in the icon thrashing as each timeout completed in quick succession. Now the timeouts are debounced so only the last one is run.

index.html

index bbde04b2c857e8c632bedd8f99fff6abe510c3f1..fae6ad48dfd34e10253dd1807a0228d50d602b44 100644 (file)
     }, 250));
 
     // Set up buttons
-    function debounceButton(button, icon, message) {
-      button.setAttribute('aria-label', 'Copied!');
+    const buttonTypes = {
+      copyPermalink: { icon: 'permalink', message: 'Copy permalink to clipboard', success: 'Copied!', id: null },
+      downloadTable: { icon: 'download', message: 'Download table to CSV', success: 'Downloaded!', id: null },
+      copyTable: { icon: 'clipboard', message: 'Copy table to clipboard', success: 'Copied!', id: null }
+    }
+    function debounceButton(button, type) {
+      const data = buttonTypes[type];
       const svg = button.querySelector('svg');
+      button.setAttribute('aria-label', data.success);
       svg.style.setProperty('stroke', 'green');
       svg.innerHTML = icons['check'];
-      setTimeout(() => {
-        svg.innerHTML = icon;
+      clearTimeout(data.id);
+      data.id = setTimeout(() => {
+        svg.innerHTML = icons[data.icon];
         svg.style.setProperty('stroke', 'currentColor');
-        button.setAttribute('aria-label', message);
+        button.setAttribute('aria-label', data.message);
       }, 2000);
     }
 
           .then(url => new Blob([url], { type: 'text/plain' }))
       })];
       navigator.clipboard.write(data);
-      debounceButton(e.target, icons['permalink'], 'Copy permalink to clipboard');
+      debounceButton(e.target, 'copyPermalink');
     });
 
     document.querySelector('#clear-code').addEventListener('click', e => {
       a.href = URL.createObjectURL(file);
       a.download = file.name;
       a.click();
-      debounceButton(e.target, icons['download'], 'Download table to CSV');
+      debounceButton(e.target, 'downloadTable');
     });
 
     document.querySelector('#copy-table').addEventListener('click', e => {
         'text/html': new Blob([`<style>table{white-space:nowrap;border-collapse:collapse;mso-ascii-theme-font:minor-latin;}th,td{mso-border-alt:solid windowtext .5pt;}thead{background:#E8E8E8;}</style>${table.outerHTML}`], { type: 'text/html' })
       })];
       navigator.clipboard.write(data);
-      debounceButton(e.target, icons['clipboard'], 'Copy table to clipboard');
+      debounceButton(e.target, 'copyTable');
     });
 
     if (location.hash !== '') {
This page took 0.043375 seconds and 4 git commands to generate.