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

This commit is contained in:
Samuel Breznjak 2025-01-25 17:08:24 +01:00 committed by GitHub
commit ad77e30568
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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);
})