Returns true if the browser tab of the page is focused, false otherwise.

Use the Document.hidden property, introduced by the Page Visibility API to check if the browser tab of the page is visible or hidden.

代码片段

const isBrowserTabFocused = () => !document.hidden;

使用样例

isBrowserTabFocused(); // true