clear existing copy code btn timeout before creating another

This commit is contained in:
Ben Gardiner 2025-01-24 18:29:44 +00:00
parent 20bb3d1912
commit cd304bba85

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