document.addEventListener('DOMContentLoaded', async function() { async function fetchLastBeat() { const heartbeatElement = document.getElementById('heartbeat-time'); try { const response = await fetch('https://quinten0508.com/api/heartbeat'); const data = await response.json(); const phoneDevice = data.find(device => device.device_name === 'Phone'); if (phoneDevice) { const phoneTimestamp = phoneDevice.last_beat.timestamp; const timestampMilliseconds = phoneTimestamp * 1000; const currentTime = Date.now(); const difference = currentTime - timestampMilliseconds; const isDead = difference > (1000 * 60 * 60 * 48); const online = difference < (1000 * 60 * 5); if (online) { heartbeatElement.textContent = 'Online!'; heartbeatElement.style = 'color: #00b400'; return; } else if (isDead) { heartbeatElement.textContent = 'Dead'; heartbeatElement.style = 'color: #b30000'; } else { heartbeatElement.textContent = 'Alive'; heartbeatElement.style = 'color: #00b400'; } } else { console.error('Device "Phone" not found.'); } } catch (error) { console.error('Error fetching last beat:', error); } } // Fetch the last beat immediately and then every 10 seconds fetchLastBeat(); setInterval(fetchLastBeat, 10000); });