Angular 7 检测浏览器选项卡已激活

标签 angular browser tabs

在我们的应用程序中,我们必须从后端服务获取用户上下文。如果用户上下文发生变化,应用程序需要重新加载。因为用户可以在不同的选项卡中更改他们的上下文。我们每 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/

相关文章:

iphone - 移动浏览器上的网页可以通过手机询问/获取用户位置吗?

python - 如何以编程方式检查 Windows 计算机上打开的浏览器实例?

tabs - 如何使nano中的制表符为4个空格而不是8个空格?

javascript - 使用新选项卡 Extjs4 传递额外参数

javascript - 使用切换函数在 ngFor 中生成的展开/折叠字段?

javascript - Angular 12+ : Is there a Simple way to apply display mask/pipe for Reactive Form?

angular - 如何使用 angular 在 s3 中使用带有多个文件的预签名 url

javascript - 以 Angular 将 map 转换为json对象

html - chrome 与所有其他浏览器的输入文本框宽度不同

php - 删除单个产品选项卡并在 Woocommerce 中添加相关内容