From 15ca223f1dadb66173aaedf85614a5c36c05cb25 Mon Sep 17 00:00:00 2001 From: ammakr Date: Thu, 5 Dec 2024 15:19:19 +0500 Subject: [PATCH] fix multiple count increase on multiple clicks --- src/components/DownloadTabs.astro | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/components/DownloadTabs.astro b/src/components/DownloadTabs.astro index dcadcc4..0ca6ee9 100644 --- a/src/components/DownloadTabs.astro +++ b/src/components/DownloadTabs.astro @@ -85,14 +85,28 @@ import { data } from "src/javascript/downloadsData"; const button = document.getElementById( `download-` + element.download_type ); - button?.addEventListener("click", (e) => { + let isRequestInProgress = false; + button?.addEventListener("click", function (e) { e.preventDefault(); - increaseCount(element.download_type); - console.log("it ran"); - setTimeout(() => { - // @ts-expect-error - window.location.href = button.getAttribute("href"); - }, 1000); + if (isRequestInProgress) { + return; + } + isRequestInProgress = true; + + try { + increaseCount(element.download_type); + console.log("it ran"); + setTimeout(() => { + // @ts-expect-error + window.location.href = button.getAttribute("href"); + }, 1000); + } catch (e) { + console.log(e); + } finally { + setTimeout(() => { + isRequestInProgress = false; + }, 3000); + } }); } );