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

前端 未结 19 2106
无人共我
无人共我 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 05:03

    I would use jQuery because then all you have to do is this:

    $(window).blur(function(){
      //your code here
    });
    $(window).focus(function(){
      //your code
    });
    

    Or at least it worked for me.

提交回复
热议问题