reduce CPU usage

This commit is contained in:
Carl Pearson
2025-05-01 05:56:04 -06:00
parent b4aaa4410c
commit 01bd11a942

View File

@@ -7,13 +7,14 @@ const videoId = document.getElementById('video-id').value;
// Get the initial timestamp from the hidden input // Get the initial timestamp from the hidden input
const initialTimestamp = parseFloat(document.getElementById('video-timestamp').value || 0); const initialTimestamp = parseFloat(document.getElementById('video-timestamp').value || 0);
// Track the last time we sent an update to avoid excessive requests // Track which media element is currently playing
let lastUpdateTime = 0; let activeMedia = null;
let updateTimer = null;
const UPDATE_INTERVAL = 5000; // 5 seconds in milliseconds const UPDATE_INTERVAL = 5000; // 5 seconds in milliseconds
// Function to send the current time to the server // Function to send the current time to the server
function sendTimestampToServer(timestamp) { function sendTimestampToServer(timestamp) {
console.log(timestamp) console.log(videoId, "->", timestamp)
fetch(`/video/${videoId}/set_timestamp`, { fetch(`/video/${videoId}/set_timestamp`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -25,13 +26,24 @@ function sendTimestampToServer(timestamp) {
}); });
} }
// Function to handle periodic updates while playing // Function to start the update timer
function handleTimeUpdate(media) { function startUpdateTimer() {
// Only send updates every 5 seconds while playing // Clear any existing timer
const now = Date.now(); stopUpdateTimer();
if (!media.paused && now - lastUpdateTime >= UPDATE_INTERVAL) {
sendTimestampToServer(media.currentTime); // Start a new timer that sends updates every 5 seconds
lastUpdateTime = now; updateTimer = setInterval(() => {
if (activeMedia && !activeMedia.paused) {
sendTimestampToServer(activeMedia.currentTime);
}
}, UPDATE_INTERVAL);
}
// Function to stop the update timer
function stopUpdateTimer() {
if (updateTimer) {
clearInterval(updateTimer);
updateTimer = null;
} }
} }
@@ -45,16 +57,43 @@ mediaElements.forEach(media => {
} }
}); });
// Send updates periodically while playing // When media starts playing
media.addEventListener('timeupdate', () => handleTimeUpdate(media)); media.addEventListener('play', () => {
// Set this as the active media
activeMedia = media;
// Start the update timer
startUpdateTimer();
});
// Send update when media is paused // When media is paused
media.addEventListener('pause', () => { media.addEventListener('pause', () => {
// Send the current timestamp
sendTimestampToServer(media.currentTime); sendTimestampToServer(media.currentTime);
// If this is the active media, stop the timer
if (activeMedia === media) {
stopUpdateTimer();
activeMedia = null;
}
}); });
// Reset timestamp when media ends // Reset timestamp when media ends
media.addEventListener('ended', () => { media.addEventListener('ended', () => {
sendTimestampToServer(0); sendTimestampToServer(0);
// If this is the active media, stop the timer
if (activeMedia === media) {
stopUpdateTimer();
activeMedia = null;
}
}); });
}); });
// Clean up when the page is unloaded
window.addEventListener('beforeunload', () => {
// Send final timestamp if media is playing
if (activeMedia && !activeMedia.paused) {
sendTimestampToServer(activeMedia.currentTime);
}
stopUpdateTimer();
});