express: time and session ID

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-09-15 06:40:33 -04:00
parent c811d35fa5
commit 44167de812
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED
6 changed files with 1196 additions and 165 deletions

View file

@ -11,6 +11,10 @@
<button id="isNotCritterButton">Is <b>not</b> Critter</button>
<script>
// Initialize session variables
let sessionStartTime;
let lastButtonClickTime;
// Function to generate a random session ID
function generateSessionId() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
@ -38,6 +42,21 @@
return sessionId;
}
// Function to set session start time
function setSessionStartTime() {
sessionStartTime = new Date();
}
// Function to calculate and display time difference
function displayTimeDifference() {
if (sessionStartTime && lastButtonClickTime) {
const timeDifference = lastButtonClickTime - sessionStartTime;
console.log(`Time since session start: ${timeDifference} milliseconds`);
// You can display the time difference on the page as needed
}
}
// Add click event listeners to the buttons
document.getElementById('isCritterButton').addEventListener('click', () => {
recordButtonClick('is critter', getSessionId());
@ -50,21 +69,29 @@
// Function to record button clicks on the server
async function recordButtonClick(buttonName, sessionId) {
try {
await fetch('/recordButtonClick', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ buttonName, sessionId }),
});
const currentTime = new Date();
if (lastButtonClickTime) {
const timeDifference = currentTime - lastButtonClickTime;
// Include the time difference in the POST request data
await fetch('/recordButtonClick', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ buttonName, sessionId, timeDifference }),
});
}
lastButtonClickTime = currentTime; // Record the timestamp of the button click
displayTimeDifference(); // Calculate and display time difference
getRandomAnimal(); // Load another random animal
} catch (error) {
console.error('Error recording button click:', error);
}
}
// Initial random animal load
// Initial random animal load and session start time
getRandomAnimal();
setSessionStartTime();
</script>
</body>
</html>