如何使用JS检查某人当前是否在网站上(在线)?

发布于 2025-02-12 19:46:21 字数 631 浏览 1 评论 0原文

我正在建立一个带有聊天等的网站等。问题是我无法正确定义某人当前在网站上,这是因为我无法设置他们的活动。

我写了一个函数,该函数由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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终弃我 2025-02-19 19:46:21

听起来您正在尝试构建一个存在系统,在这种情况下,我建议您在管理在场如何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.

彡翼 2025-02-19 19:46:21

您是否正在尝试确定页面是否已加载?如果是这样,那么一个简单的jQuery文档加载将执行

https:// 。

  <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $( window ).on( "load", function() {
        console.log( "window loaded" );
    });
//or
$(function() {
    console.log( "ready!" );
});
    </script>

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/

  <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
    $( window ).on( "load", function() {
        console.log( "window loaded" );
    });
//or
$(function() {
    console.log( "ready!" );
});
    </script>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文