aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatarLarge Libravatar Samuel Breznjak <samuelbreznjak35gmailcom>2025-01-25 17:08:24 +0100
committerLibravatarLarge Libravatar GitHub <noreplygithubcom>2025-01-25 17:08:24 +0100
commitad77e305686907fb380e9a6bb6ccde029882b545 (patch)
tree8feea883cdb96d775d4421420a5861d7a3bbb354
parent20bb3d19125a0d9d6e54de23c0b704c56c923c73 (diff)
parentcd304bba851ce05e7f09c18a86db4b4a64f8b044 (diff)
downloadcs16-defaults.css-ad77e305686907fb380e9a6bb6ccde029882b545.tar.gz
cs16-defaults.css-ad77e305686907fb380e9a6bb6ccde029882b545.tar.bz2
cs16-defaults.css-ad77e305686907fb380e9a6bb6ccde029882b545.tar.lz
cs16-defaults.css-ad77e305686907fb380e9a6bb6ccde029882b545.zip

Clear existing "copy code" button timeout before creating another (#2)

-rw-r--r--src/main.js9
1 files changed, 8 insertions, 1 deletions
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);
})