在我们的应用程序中,我们必须从后端服务获取用户上下文。如果用户上下文发生变化,应用程序需要重新加载。因为用户可以在不同的选项卡中更改他们的上下文。我们每 5 秒 ping 一次后端服务,以检查用户上下文是否已更改。
有没有办法检测用户是否停用或激活了当前选项卡?这将节省每 5 秒对后端执行一次 ping 操作。
用户可以在同一个应用程序或另一个应用程序的不同选项卡中更改上下文。
最佳答案
使用 hasFocus() 方法来自:here , 它存储选项卡焦点的上下文。
The hasFocus() method of the Document interface returns a Boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.
如果你想要一个带有事件监听器的异步版本,请使用 Page Visibility API .
关于Angular 7 检测浏览器选项卡已激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54597540/