How to detect in iOS webapp when switching back to Safari from background?

后端 未结 7 709
栀梦
栀梦 2020-11-29 23:29

How can I build a webpage which is able to monitor when the page gets the focus, especially when Safari is in the background and the user switches Safari back to the foregro

7条回答
  •  执念已碎
    2020-11-30 00:09

    I believe timers (setInterval()) are suspended when the app enters the background. You could do something like:

    var lastFired = new Date().getTime();
    setInterval(function() {
        now = new Date().getTime();
        if(now - lastFired > 5000) {//if it's been more than 5 seconds
            alert("onfocus");
        }
        lastFired = now;
    }, 500);
    

    You may need to adjust those time intervals to suite your needs.

    But, most likely, if it has been long enough to need a refresh (a few days) safari will probably reload the page because it is out of memory.

提交回复
热议问题