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