Prevent iOS mobile safari from going idle / auto-locking / sleeping?

后端 未结 6 785
星月不相逢
星月不相逢 2020-12-04 10:32

In an iOS app you can set application.idleTimerDisabled = YES to prevent the phone from auto locking.

I need to do this in mobile safari for a game li

6条回答
  •  醉话见心
    2020-12-04 11:20

    You can stop sleeping and screen dimming in iOS Safari by faking a refresh every 20–30 seconds

    var stayAwake = setInterval(function () {
        location.href = location.href; //try refreshing
        window.setTimeout(window.stop, 0); //stop it soon after
    }, 30000);
    

    Please use this code responsibly, don't use it "just because". If it's only needed for a bit, disable it.

    clearInterval(stayAwake); //allow device sleep again when not needed
    

    Tested in Safari iOS 7, 7.1.2, and 8.1, but it may not work in UIWebView browsers like Chrome for iOS or the Facebook app.

    Demo: http://jsbin.com/kozuzuwaya/1

提交回复
热议问题