Description: On the status bar, visitors stay on this page (such as: You stay on this page for x hours x minutes x seconds)
The problem is to design a timer that shows how long the viewer has stayed on the page. To solve this problem, I think of the main two ways.
Method One: Use the system time. That is, set a variable to get the logon start time StartTime, and then use the settimeout () function to keep the page constantly refreshed, while refreshing, get the current time nowtime, and then use the current time minus the logon start time, that is, stay time. Do not write in detail here. The following methods are used to realize the next point.
Method Two: Set three variables: Second,minute,hour. Then let second Non-stop +1, and use settimeout to implement the page every second refresh once, when second is greater than or equal to 60 o'clock, minute start +1, and let second reset zero. Similarly, when minute is greater than or equal to 60 o'clock, hour begins +1. This enables the timing function.
Method two code is as follows:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<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 stay on this page" +hour+ "hours" +minute+ "minutes" +second+ "seconds";
}
</script>
</body>
The effect is as shown in the figure.