zoukankan      html  css  js  c++  java
  • 手机端黑屏时定时器无法执行

    最近在写手机端页面时,发现当手机端黑屏时,当前页面的定时器停止执行了。

    一开始的思路是,h5也会有想小程序的onShow,onHide的钩子函数的事件可以监听。但是经过各种实验也没能找到这样的事件。

    so,开始了解为什么黑屏时定时器停止。经过一系列的百度之后,明白在是ios的为了安全考虑。

    You can't. Web pages do not execute javascript unless (1) the page is frontmost and (2) Safari is actually active. Safari does not remain active when the device is asleep.
    
    If you think about it, this functionality would be a massive privacy breach. Imagine how you'd feel if you visited a web page that then secretly tracked your location even when your device was off! You'd be very angry. Even if you could do this technologically, people may be upset if they found out you did it.
    
    Location info is closely guarded for reasons of privacy and physical safety (think about stalkers and your web page.) That is why you get a dialog asking to use the Location Manager the first time you get a location.

    既然ios会强行将关闭定时器,于是也找到了方法,用iframe在即使手机被锁定的时候也让js定时执行。

    在需要定时器的页面加个iframe

    <iframe src="background.html" style="display:none;"></iframe>

    iframe代码

    <!DOCTYPE HTML>
    <head>
    <meta http-equiv="refresh" content="3" />
    </head>
    <body>
    <script>
    parent.run_function_from_core_page();
    </script>
    </body>
    </html>

    这样可以愉快的进行定时任务了

  • 相关阅读:
    二十三、java连接oracle数据库操作:jdbc
    四、正则表达式使用
    Linux常用命令大全
    消息队列的常见问题
    JVM:带你查看常见的问题,以及分析处方法
    JVM监控与调优
    缓存总结2
    缓存总结1
    消息队列mq总结
    Java集合Map基本方法
  • 原文地址:https://www.cnblogs.com/tzzf/p/9123855.html
Copyright © 2011-2022 走看看