Skip to content

Commit

Permalink
fix: made a visitor counter api and linked it to the app
Browse files Browse the repository at this point in the history
  • Loading branch information
mondalsurojit committed Jun 21, 2024
1 parent 1f06995 commit 97862ae
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions 14 - Visit Counter/script.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 97862ae

Please sign in to comment.