如何使用JS检查某人当前是否在网站上(在线)?
我正在建立一个带有聊天等的网站等。问题是我无法正确定义某人当前在网站上,这是因为我无法设置他们的活动。
我写了一个函数,该函数由html文件中的身体标签调用< body onload =“ changestatus(true)” onunload =“ changestatus(false)(false)”>
,但正如我所说的那样,这似乎不是工作。
当第一个用户的状态设置为在线值时,第二个用户的状态立即更改为离线。当我尝试在网站上的输入中键入某些内容时,它也将状态更改为离线,并不重要。
我目前正在考虑仅使用会议就将此部分写在PHP中,但是也许有人知道如何在JS中执行此操作?
function changeStatus(event) {
if (event) {
db.ref("status/" + username + "/" + receiver).set({
act: "online"
});
} else {
db.ref("status/" + username + "/" + receiver).set({
act: "offline"
});
}
}
I am building a website with chat etc. The problem is I cannot correctly define if someone is currently on the website which cause I cannot set their activity.
I wrote a function which is invoked by the body tag in HTML file <body onload="changeStatus(true)" onunload="changeStatus(false)">
but as I said it seems not to work.
When the status of the first users sets to the online value, the second one immediately changes to offline. When I try to type something in the input on the website it also changes the status to offline, it doesn't matter who is typing.
I am currently thinking of writing this part in PHP just using the session, but maybe someone knows how to do this in JS?
function changeStatus(event) {
if (event) {
db.ref("status/" + username + "/" + receiver).set({
act: "online"
});
} else {
db.ref("status/" + username + "/" + receiver).set({
act: "offline"
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在尝试构建一个存在系统,在这种情况下,我建议您在管理在场,如何
OnDisconnect
working,检测连接状态 href =“ https://firebase.google.com/docs/database/web/web/offline-capabilities#section-sample-sample” rel =“ nofollow noreferrer”>示例存在app app 。
It sounds like you're trying to build a presence system, in which case I recommend checking out the Firebase documentation on managing presence, how
onDisconnect
works, detecting connection state and the sample presence app.您是否正在尝试确定页面是否已加载?如果是这样,那么一个简单的jQuery文档加载将执行
https:// 。
https://api.jquery.com/mouseover/
https://api.jquery.com/click/
我希望这会有所帮助
Are you trying to determine if the page has loaded? If so then a simple jquery document load on will do that
https://learn.jquery.com/using-jquery-core/document-ready/
if your trying to capture if the user is on the page then mouse over or mouse click might be better
https://api.jquery.com/mouseover/
https://api.jquery.com/click/
I hope this helps