Home  >  Article  >  Web Front-end  >  Use js to display the time the visitor stayed on this page in the browser status bar_javascript skills

Use js to display the time the visitor stayed on this page in the browser status bar_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:06:171586browse

Problem description: Display on the status bar, the time the visitor stayed on this page (for example: you stayed on this page (for example: you stayed on this page for X hours, minutes and 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 I can think of:

Method 1: Use the 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 Dwell time. I won’t write it in detail here. Let’s focus on the second method used below.

Method two: set three variables: second, minute, hour. 1, and use setTimeout to refresh the page every second. When second is greater than or equal to 60, minute starts with 1, and second is reset to zero. Similarly, when minute is greater than or equal to 60, hour starts with 1. This can realize the timing function. .

The code for method two is as follows:

Copy the code The code is as follows:


< ;html xmlns="http://www.w3.org/1999/xhtml">


Untitled Document






Running effect As shown in the picture.
Use js to display the time the visitor stayed on this page in the browser status bar_javascript skills
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn