Is there a way to detect if a browser window is not currently active?

前端 未结 19 2577
无人共我
无人共我 2020-11-21 04:40

I have JavaScript that is doing activity periodically. When the user is not looking at the site (i.e., the window or tab does not have focus), it\'d be nice to not run.

19条回答
  •  轮回少年
    2020-11-21 04:48

    In HTML 5 you could also use:

    • onpageshow: Script to be run when the window becomes visible
    • onpagehide: Script to be run when the window is hidden

    See:

    • https://developer.mozilla.org/en-US/docs/Web/Events/pageshow
    • https://developer.mozilla.org/en-US/docs/Web/Events/pagehide

提交回复
热议问题