帮我加上在线访问人数,每5分钟刷新一次在线人数
时间: 2023-07-06 12:44:03 浏览: 65
好的,以下是一个可以显示在线访问人数的HTML代码,并且每隔5分钟刷新一次在线人数:
```html
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<script>
// 获取当前日期,格式为 yyyy-mm-dd
var today = new Date().toISOString().slice(0, 10);
// 从本地存储中获取当天访问次数,如果不存在则初始化为0
var todayCount = parseInt(localStorage.getItem(today)) || 0;
// 增加当天访问次数
todayCount++;
// 将当天访问次数存储到本地存储中
localStorage.setItem(today, todayCount);
// 从本地存储中获取累计访问人数,如果不存在则初始化为0
var totalCount = parseInt(localStorage.getItem("total")) || 0;
// 增加累计访问人数
totalCount++;
// 将累计访问人数存储到本地存储中
localStorage.setItem("total", totalCount);
</script>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>当天访问次数: <span id="todayCount"></span></p>
<p>累计访问人数: <span id="totalCount"></span></p>
<p>在线访问人数: <span id="onlineCount"></span></p>
<script>
// 每隔5分钟刷新在线访问人数
setInterval(function() {
// 从服务器获取在线访问人数
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("onlineCount").innerHTML = this.responseText;
}
};
xhttp.open("GET", "online.php", true);
xhttp.send();
}, 5 * 60 * 1000);
// 将当天访问次数和累计访问人数显示在页面中
document.getElementById("todayCount").innerHTML = todayCount;
document.getElementById("totalCount").innerHTML = totalCount;
</script>
</body>
</html>
```
以上代码中,我们添加了一个新的元素`<p>`来显示在线访问人数。然后,我们使用`setInterval`函数来每隔5分钟刷新在线访问人数。在刷新在线人数时,我们向服务器发送一个GET请求,并将服务器返回的在线访问人数显示在页面中。最后,我们将当天访问次数和累计访问人数显示在页面中。需要注意的是,你需要在服务器上创建一个PHP脚本`online.php`来实现在线访问人数的获取。