From cd304bba851ce05e7f09c18a86db4b4a64f8b044 Mon Sep 17 00:00:00 2001 From: Ben Gardiner Date: Fri, 24 Jan 2025 18:29:44 +0000 Subject: clear existing copy code btn timeout before creating another --- src/main.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index 3ca7d30..44eab32 100644 --- a/src/main.js +++ b/src/main.js @@ -9,6 +9,8 @@ document.querySelectorAll(".toggle-vision").forEach((button) => { }); document.querySelectorAll(".inner .cs-btn").forEach((button) => { + let timeout = null; + button.addEventListener("click", function () { const codeElement = this.closest(".inner").querySelector("code"); const codeToCopy = codeElement.textContent; @@ -16,7 +18,12 @@ document.querySelectorAll(".inner .cs-btn").forEach((button) => { .writeText(codeToCopy) .then(() => { this.textContent = "Copied!"; - setTimeout(() => { + + if (timeout !== null) { + clearTimeout(timeout); + } + + timeout = setTimeout(() => { this.textContent = "Copy"; }, 2000); }) -- cgit v1.2.3