From 97862ae117b20935a4b1718e9c6a1c3a731ff71b Mon Sep 17 00:00:00 2001 From: surojitmondal Date: Sat, 22 Jun 2024 01:44:16 +0530 Subject: [PATCH] fix: made a visitor counter api and linked it to the app --- 14 - Visit Counter/script.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/14 - Visit Counter/script.js b/14 - Visit Counter/script.js index fc746fe2..c0e5b913 100644 --- a/14 - Visit Counter/script.js +++ b/14 - Visit Counter/script.js @@ -1,27 +1,27 @@ const countEl = document.getElementById('count'); -updateVisitCount(); +document.addEventListener('DOMContentLoaded', function () { + updateVisitCount(); +}); function updateVisitCount() { - fetch('https://api.countapi.xyz/hit/swapnilsparsh/?amount=1') - .then(res => res.json()) - .then(res => { - countEl.innerHTML = res.value; - animateValue(countEl, 0,res.value, 1000); - } - - ) + fetch('https://visitor-counter-api.netlify.app/.netlify/functions/counter/') + .then(res => res.json()) + .then(res => { + countEl.innerHTML = res.value; + animateValue(countEl, 0, res.value, 1000); + }) } function animateValue(obj, start, end, duration) { let startTimestamp = null; const step = (timestamp) => { - if (!startTimestamp) startTimestamp = timestamp; - const progress = Math.min((timestamp - startTimestamp) / duration, 1); - obj.innerHTML = Math.floor(progress * (end - start) + start); - if (progress < 1) { - window.requestAnimationFrame(step); - } + if (!startTimestamp) startTimestamp = timestamp; + const progress = Math.min((timestamp - startTimestamp) / duration, 1); + obj.innerHTML = Math.floor(progress * (end - start) + start); + if (progress < 1) { + window.requestAnimationFrame(step); + } }; window.requestAnimationFrame(step); - } \ No newline at end of file +} \ No newline at end of file