screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <div id="app"> <h1>Javascript Measure Time Spent on Page</h1> <p>Time Spent on Page: <span id="timeDisplay">00:00</span></p> </div> <script> const timeDisplay = document.getElementById('timeDisplay'); let time = 0, intervalId = null, isActive = true; let pageLoadTime = window.performance.now(); const updateFormattedTime = () => { const minutes = Math.floor(time / 60000), seconds = ((time % 60000) / 1000).toFixed(0); timeDisplay.textContent = `${minutes}:${(seconds < 10 ? "0" : "")}${seconds}`; }; intervalId = setInterval(() => { if (isActive) { const currentTime = window.performance.now(); time = currentTime - pageLoadTime; updateFormattedTime(); } }, 1000); document.addEventListener("visibilitychange", () => { isActive = !document.hidden; if (isActive) { pageLoadTime = window.performance.now() - time; } }); </script> </body> </html>