Home > Article > Web Front-end > Use js to display the time the visitor stayed on this page in the browser status bar_javascript skills
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: