Is it possible, in JavaScript, to detect when the screen is turned off in the Android & iOS browsers

后端 未结 4 816
暖寄归人
暖寄归人 2020-12-03 17:06

I was tracking down some ridiculously high load times that my app\'s javascript reported, and found that Android (and iOS) pause some JavaScript execution when the window is

4条回答
  •  余生分开走
    2020-12-03 17:46

    There is few options to check it:

    1. Using Visibility API

    2. Using focus and blur events to detect browser tab visibility:

    window.addEventListener("focus", handleBrowserState.bind(context, true));
    window.addEventListener("blur", handleBrowserState.bind(context, false));
    
    function handleBrowserState(isActive){
        // do something
    }
    
    1. Using timers, as mentioned above

提交回复
热议问题