Problem description: Display on the status bar the time the visitor has stayed on this page (for example: you have stayed on this page (for example: you have stayed on this page for X hours, X minutes, X seconds)
This problem is equivalent to designing a timer to show how long the viewer has stayed on the page. To solve this problem, there are two main methods that come to my mind.
Method 1: Use system time. That is, first set a variable to get the login start time startTime, and then use the setTimeout() function to refresh the page continuously. While refreshing, get the current time nowTime, and then subtract the login start time from the current time, which is the stay. time. It will not be written in detail here. Let’s focus on the second method used below to implement it.
Method 2: Set three variables: second, minute, hour. Then let second keep increasing by 1, and use setTimeout to refresh the page every second. When second is greater than or equal to 60, minute starts to increase by 1, and second is reset to zero. Similarly, when minute is greater than or equal to 60, hour starts to +1. In this way, the timing function can be realized.
The code for method two is as follows:
Copy the code code as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body onload="timeCount()">
<script type="text/javascript">
var second=0;
var minute=0;
var hour=0;
function timeCount(){
second=second+1;
setTimeout("timeCount()",1000);
while(second>=60){
minute=minute+1;
second=0;
while(minute>=60){
hour=hour+1;
minute=0;
second=0;
}
}
window.status="You have stayed on this page"+hour+"hour"+minute+"minute"+second+"second";
}
</script>
</body>
</html>
The running effect is shown in the figure.