From 809091b0fb1a3edb691ad41309503bbaad66c872 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Thu, 17 Apr 2025 23:30:59 -0700 Subject: [PATCH] Actually debounce button icon changes 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 | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index bbde04b..fae6ad4 100644 --- a/index.html +++ b/index.html @@ -444,15 +444,22 @@ }, 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); } @@ -463,7 +470,7 @@ .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 => { @@ -480,7 +487,7 @@ 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 => { @@ -489,7 +496,7 @@ 'text/html': new Blob([`${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 !== '') { -- 2.47.3