Using JS implementation in the browser status bar to display the time visitors stay on this page _javascript tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.