Javascript DOM Body onload event handler
<html> <head> <title>onload Event</title> <script type = "text/javascript"> let seconds = 0;//w ww . ja v a 2s. c om // called when the page loads to begin the timer function startTimer() { // 1000 milliseconds = 1 second window.setInterval( "updateTime()", 1000 ); } // called every 1000 ms to update the timer function updateTime() { ++seconds; document.getElementById( "soFar" ).innerHTML = seconds; } </script> </head> <body onload = "startTimer()"> <p>Seconds you have spent viewing this page so far: <strong id = "soFar">0</strong></p> </body> </html>