Skip to content

Commit

Permalink
refactor: simplify visitor count data handling and display logic #14
Browse files Browse the repository at this point in the history
- Remove caching mechanism for visitor count data
- Fetch and update visitor count on every page load
- Adjust display logic to hide counters before fetching and show after
- Remove immediate display of stored data on page load
  • Loading branch information
EvanNotFound committed Nov 30, 2024
1 parent dbd83dc commit 381ff51
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion public/js/client.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 6 additions & 13 deletions src/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var visitorCounterCaller, visitorCounterDisplay;
const baseUrl = getBaseUrl();
const apiUrl = `${baseUrl}/log?jsonpCallback=VisitorCountCallback`;
try {
visitorCounterDisplay.hideAll();

const response = await fetch(apiUrl, {
method: "POST",
headers: {
Expand All @@ -43,10 +45,9 @@ var visitorCounterCaller, visitorCounterDisplay;
const data = await response.json();
documentReady(() => {
callback(data);
// Store new values in localStorage
localStorage.setItem("visitorCountData", JSON.stringify(data));
visitorCounterDisplay.showAll();
});
visitorCounterDisplay.showAll();
} catch (error) {
console.error("Error fetching visitor count:", error);
visitorCounterDisplay.hideAll();
Expand Down Expand Up @@ -111,17 +112,9 @@ var visitorCounterCaller, visitorCounterDisplay;
},
};

// Load and display stored data immediately
documentReady(() => {
const storedData = localStorage.getItem("visitorCountData");
if (storedData) {
visitorCounterDisplay.updateText(JSON.parse(storedData));
visitorCounterDisplay.showAll();
}
visitorCounterCaller.fetch(
visitorCounterDisplay.updateText.bind(visitorCounterDisplay)
);
});

// Fetch and update visitor count data
visitorCounterCaller.fetch(
visitorCounterDisplay.updateText.bind(visitorCounterDisplay),
);
})();

0 comments on commit 381ff51

Please sign in to comment.