2023-09-15 11:02:14 +00:00
|
|
|
// Initialize session variables
|
|
|
|
let sessionStartTime;
|
|
|
|
let lastButtonClickTime;
|
|
|
|
|
|
|
|
// Function to fetch a random animal name from the server
|
2023-09-15 12:26:01 +00:00
|
|
|
async function getNextAnimal() {
|
2023-09-15 14:42:37 +00:00
|
|
|
try {
|
|
|
|
const response = await fetch("/getNextAnimal");
|
|
|
|
const data = await response.json();
|
|
|
|
document.getElementById("animal-name").textContent = data.animalName;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching data:", error);
|
|
|
|
}
|
2023-09-15 11:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Function to set or retrieve the session ID cookie
|
|
|
|
function getSessionId() {
|
2023-09-15 14:42:37 +00:00
|
|
|
const sessionId = document.cookie.replace(
|
|
|
|
/(?:(?:^|.*;\s*)sessionId\s*=\s*([^;]*).*$)|^.*$/,
|
|
|
|
"$1",
|
|
|
|
);
|
|
|
|
if (!sessionId) {
|
2023-09-29 01:19:58 +00:00
|
|
|
return newSession();
|
2023-09-15 14:42:37 +00:00
|
|
|
}
|
|
|
|
return sessionId;
|
2023-09-15 11:02:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 01:19:58 +00:00
|
|
|
async function newSession() {
|
2023-09-15 14:42:37 +00:00
|
|
|
setSessionStartTime();
|
2023-09-29 01:19:58 +00:00
|
|
|
try {
|
|
|
|
const response = await fetch("/newSession");
|
|
|
|
const data = await response.json();
|
|
|
|
document.getElementById("animal-name").textContent = data.animalName;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching data:", error);
|
|
|
|
}
|
2023-09-15 14:42:37 +00:00
|
|
|
getSessionId();
|
2023-09-15 12:26:01 +00:00
|
|
|
}
|
2023-09-15 11:02:14 +00:00
|
|
|
|
|
|
|
// Function to set session start time
|
|
|
|
function setSessionStartTime() {
|
2023-09-15 14:42:37 +00:00
|
|
|
sessionStartTime = new Date();
|
2023-09-15 11:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Function to calculate and display time difference
|
|
|
|
function displayTimeDifference() {
|
2023-09-15 14:42:37 +00:00
|
|
|
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
|
|
|
|
}
|
2023-09-15 11:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Function to record button clicks on the server
|
|
|
|
async function recordButtonClick(buttonName, sessionId) {
|
2023-09-15 14:42:37 +00:00
|
|
|
try {
|
|
|
|
const currentTime = new Date();
|
|
|
|
if (lastButtonClickTime) {
|
|
|
|
const timeDifference = currentTime - lastButtonClickTime;
|
|
|
|
// Include the time difference in the POST request data
|
|
|
|
const animal = document.getElementById("animal-name").textContent;
|
|
|
|
const bodyData = JSON.stringify({
|
|
|
|
animal: animal,
|
|
|
|
button: buttonName,
|
|
|
|
session: sessionId,
|
|
|
|
difference: timeDifference,
|
|
|
|
time: sessionStartTime,
|
|
|
|
});
|
|
|
|
//console.log(bodyData);
|
|
|
|
await fetch("/recordButtonClick", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: bodyData,
|
|
|
|
});
|
2023-09-15 11:02:14 +00:00
|
|
|
}
|
2023-09-15 14:42:37 +00:00
|
|
|
lastButtonClickTime = currentTime; // Record the timestamp of the button click
|
|
|
|
displayTimeDifference(); // Calculate and display time difference
|
|
|
|
// TODO - slight delay before loading next animal, to show the user how long that decision took them
|
|
|
|
getNextAnimal(); // Load another random animal
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error recording button click:", error);
|
|
|
|
}
|
2023-09-15 11:02:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-29 01:19:58 +00:00
|
|
|
// Add click event listeners to the buttons
|
|
|
|
document.getElementById("isCritterButton").addEventListener("click", () => {
|
|
|
|
recordButtonClick("is critter", getSessionId());
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("isNotCritterButton").addEventListener("click", () => {
|
|
|
|
recordButtonClick("is not critter", getSessionId());
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("startOverButton").addEventListener("click", () => {
|
|
|
|
newSession();
|
|
|
|
getNextAnimal();
|
|
|
|
});
|
|
|
|
|
2023-09-15 11:02:14 +00:00
|
|
|
// Initial random animal load and session start time
|
2023-09-15 12:26:01 +00:00
|
|
|
getNextAnimal();
|
2023-09-15 11:02:14 +00:00
|
|
|
setSessionStartTime();
|